[hackathon] An article about metaprogramming

Idan Arye via Digitalmars-d digitalmars-d at puremagic.com
Thu Apr 30 02:58:20 PDT 2015


On Wednesday, 29 April 2015 at 18:13:10 UTC, Mafi wrote:
> On Wednesday, 29 April 2015 at 16:55:58 UTC, bearophile wrote:
>> Mafi:
>>
>>> https://marfisc.wordpress.com/2015/04/29/using-d-templates-for-gamedev/
>>>
>>> What do you think? Any remarks?
>>
>>>The SDL_Event is a union. Accessing it is inherently unsafe 
>>>for type consistency and memory safety. The SDL library 
>>>mitigates this problem by adding a tag (the member type) which 
>>>encodes which union-member is to be used.<
>>
>> In Rust you use the built-in tagged unions and call it a day...
>
> Well, it is an already existing C library I am only adapting. 
> And additionally the shonwn template encapsulates the whole 
> outer loop as well.
>
>>
>>[...]
>>
>> The default should be aligned just like the other cases. Often 
>> is a good idea to use "final switch" with enumerations.
>> Probably there are ways to make that code more dry, using a 
>> TypeTuple of pairs like ("SDL_QUIT", "quit").
>>
>> Bye,
>> bearophile
>
> I cannot use a final switch there because it is not enumerated 
> type. The values are just constants. Moreover I want to skip 
> unneeded labels and instead unify them into the default branch. 
> This is impossible with a final switch.

If you invest some effort in redefining the event types as D 
structs and the event codes as a D enum, you can get rid of that 
routing-list construct altogether: 
http://dpaste.dzfl.pl/1e992d248355


More information about the Digitalmars-d mailing list