www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - onDispatch demo not compiling

reply "Shachar" <shachar shemesh.biz> writes:
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);
}
Aug 20 2014
next sibling parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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 =3D underscoresToCamelCase("hello_world"); then compiler will tell you why it can't evaluate the function.
Aug 20 2014
parent reply "Shachar" <shachar shemesh.biz> writes:
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
Aug 20 2014
parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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=20
 "a.opDispatch!"do_something_cool"(5, 6)", which brought most of=20
 those issues to light.
ah, silly me. i forgot about such simple thing.
Aug 20 2014
prev sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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=3D"~m);
    enum t =3D underscoresToCamelCase(t);
    pragma(msg, "t=3D"~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.
Aug 20 2014