Thread overview |
---|
August 21, 2014 onDispatch demo not compiling | ||||
---|---|---|---|---|
| ||||
I'm trying to compile the onDispatch demo program from "The D Programming Language" (page 387). At first I had an import problem, but I fixed that. Now, however, when I try to call "a.do_something_cool", I get an error message saying: onDispatch.d(43): Error: no property 'do_something_cool' for type 'onDispatch.A' Full program follows (into onDispatch.d): import core.stdc.ctype; import std.stdio; string underscoresToCamelCase(string sym) { string result; result.reserve(sym.length); bool makeUpper; foreach (c; sym) { if (c == '_') { makeUpper = true; } else { if (makeUpper) { result ~= toupper(c); makeUpper = false; } else { result ~= c; } } } return result; } unittest { assert(underscoresToCamelCase("hello_world") == "helloWorld"); assert(underscoresToCamelCase("_a") == "A"); assert(underscoresToCamelCase("abc") == "abc"); assert(underscoresToCamelCase("a_bc_d_") == "aBcD"); } class A { auto onDispatch(string m, Args...)(Args args) { // return mixin("this."~underscoresToCamelCase(m)~"(args)"); } void doSomethingCool(int x, int y) { writeln("Do something cool"); } } unittest { auto a = new A; a.doSomethingCool(5, 6); a.do_something_cool(5, 6); } |
August 21, 2014 Re: onDispatch demo not compiling | ||||
---|---|---|---|---|
| ||||
Posted in reply to Shachar Attachments: | On Thu, 21 Aug 2014 05:39:14 +0000 Shachar via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > auto onDispatch(string m, Args...)(Args args) first: opDispatch, not onDispatch. second: underscoresToCamelCase() can't be evaluated in compile-time anymore. the necessary changes: 1. add 'import std.string' -- we need toUpper() from this module. 2. change toupper(c) to toUpper(c) (first thing why ctfe fails) 3. remove 'result.reserve(sym.length);' line (second thing why ctfe fails). 4. uncomment 'return' in opDispatch. %-) that will do the trick. to check if underscoresToCamelCase() can be evaluated in compile time, you can add this line to the first unittest block: enum t = underscoresToCamelCase("hello_world"); then compiler will tell you why it can't evaluate the function. |
August 21, 2014 Re: onDispatch demo not compiling | ||||
---|---|---|---|---|
| ||||
Posted in reply to Shachar Attachments: | On Thu, 21 Aug 2014 05:39:14 +0000 Shachar via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: that's it: failing to evaluate opDispatch() template is not a compilation error. compiler will silently try to find direct method if opDispatch() fails. so be very careful with it. you can add pragma(msg, "...") to your opDispatch to see if it really compiles without errors. i.e. auto onDispatch(string m, Args...)(Args args) { pragma(msg, "m="~m); enum t = underscoresToCamelCase(t); pragma(msg, "t="~t); return mixin("this."~t~"(args)"); } you should see output of this pragmas in compile-time. if you can't see "t", for example, it means that evaluation of underscoresToCamelCase() failed. |
August 21, 2014 Re: onDispatch demo not compiling | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Thursday, 21 August 2014 at 06:11:06 UTC, ketmar via Digitalmars-d-learn wrote: > that will do the trick. > Indeed. I ended up simply directly calling "a.opDispatch!"do_something_cool"(5, 6)", which brought most of those issues to light. Shachar |
August 21, 2014 Re: onDispatch demo not compiling | ||||
---|---|---|---|---|
| ||||
Posted in reply to Shachar Attachments: | On Thu, 21 Aug 2014 06:34:10 +0000 Shachar via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > Indeed. I ended up simply directly calling "a.opDispatch!"do_something_cool"(5, 6)", which brought most of those issues to light. ah, silly me. i forgot about such simple thing. |
Copyright © 1999-2021 by the D Language Foundation