Crazy code by Adam on SO

Shammah Chancellor via Digitalmars-d digitalmars-d at puremagic.com
Thu Sep 25 21:26:09 PDT 2014


On 2014-08-29 16:48:51 +0000, Andrei Alexandrescu said:

> Worth a look:
> 
> http://stackoverflow.com/questions/25555329/d-finding-all-functions-with-certain-attribute 
> 
> 
> Andrei

That's some pretty neat code, I did something similar awhile ago for 
deserializing classes.  Here's a reduced case of it:

http://dpaste.dzfl.pl/4fffa339368f

The facility to be able to create such a factory through compile time 
reflection is absolutely awesome.  However, it "feels" as though the 
ability to do this is making use of undefined behavior rather than 
actual language features.

First:

I consider it sort of a language wart that alias hack(alias T) = T; is 
necessary for aliasing to the particular symbol, and that the 
__traits(getMember) expression can't be substituted in to the same 
places as the alias to it without compiler errors.   It's very 
non-intuitive.  I would have never figured that out on my own with out 
some other D wizards helping me about a year ago.  I still don't 
understand why I can't simply do :
		alias member = __traits(getMember, ...);

Also:

The way that the foreach works in the pasted example is very 
surprising.   I would be nice if this was a "static foreach" instead of 
just a foreach which happens to run at compile time over a TypeTuple.   
 Normally doing a switch inside a foreach like that would not work at 
all, but near as I can tell the compiler unrolls the whole thing since 
it's inputs are known at compile time.

Finally:

If you actually want to have something that returns a TypeTuple!() of 
only subclasses which you can foreach over in other places -- you have 
to make a *very* ugly recursive template:

	(Line 208 thru Line 235 returns a TypeTuple of all subclasses of the 
Message class)
	
	https://github.com/schancel/gameserver/blob/master/source/messages/core.d#L208

Making these awesome feature more intuitive and easy to use would go a 
long way.  I feel like this should be on your short list along with the 
GC and C++ compatibility.

-S.



More information about the Digitalmars-d mailing list