Compile-time variables

aliak something at something.com
Fri Apr 6 12:45:05 UTC 2018


On Friday, 6 April 2018 at 02:20:29 UTC, Kayomn wrote:
>
> Wrong example code, here's the correct example:
> --------------------------------------------------------------------------------
> switch (queryString(childJson,"type")) {
> 	case (typeof (Sprite).name):
> 		child = this.addChild!(Sprite)(childName);
>
> 		break;
>
> 	case (typeof (Camera).name):
> 		child = this.addChild!(Camera)(childName);
>
> 		break;
>
>  	default:
>  		child = this.addChild!(Node)(childName);
>
> 		break;
> }
> --------------------------------------------------------------------------------

Hi, could you show a bit more implementation details in the C++ 
version that works? Maybe someone can translate that to the 
appropriate D version?

The problem seems to be that switching on a runtime value and 
pattern matching with a compile time value is hard to pull off 
automatically without some sort of bridge (or of course I've 
misunderstood this exercise)

So if you had:

class R {
   string type() { return R.stringof; }
}
class A: R {
   override string type() { return A.stringof; }
}
class B: R {
   override string type() { return B.stringof; }
}

string type(T: R)() {
     return T.stringof;
}

void main() {
   R node = new A;
   final switch (node.type) {
   case type!R: writeln("R"); break;
   case type!A: writeln("A"); break;
   case type!B: writeln("B"); break;
   }
}

(maybe not a good idea to use stringof though, typeid probably 
better)

Cheers


More information about the Digitalmars-d-learn mailing list