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); }
Re: onDispatch demo not compiling
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
Why is this pure?
The following program compiles, and does what you'd expect: struct A { int a; } pure int func( ref A a ) { return a.a += 3; } As far as I can tell, however, it shouldn't. I don't see how or why func can possibly be considered pure, as it changes a state external to the function. What am I missing? Or is this just a compiler bug? Shachar
Why does this not work?
void func( int c ) { ubyte a; a = cast(ubyte)c + cast(ubyte)c; } dmd v2.065 complains about: test.d(5): Error: cannot implicitly convert expression (cast(int)cast(ubyte)c + cast(int)cast(ubyte)c) of type int to ubyte As far as I can tell, adding two values of the same type should result in the same type. Shachar
Re: Why does this not work?
On Wednesday, 17 September 2014 at 13:03:05 UTC, flamencofantasy wrote: the result of ubyte + ubyte is int I believe. Try; void func( int c ) { ubyte a; a = cast(ubyte)(cast(ubyte)c + cast(ubyte)c); } From http://dlang.org/type, under Usual Arithmetic Conversions: 4. Else the integer promotions are done on each operand, followed by: 1. If both are the same type, no more conversions are done. So, as far as I can see, the specs disagree with you. Shachar