digitalmars.D - [hackathon] An article about metaprogramming
- Mafi (10/10) Apr 29 2015 Hello there,
- Andrei Alexandrescu (6/15) Apr 29 2015 https://www.reddit.com/r/programming/comments/34ah1q/using_d_templates_f...
- bearophile (32/38) Apr 29 2015 In Rust you use the built-in tagged unions and call it a day...
- Mafi (8/24) Apr 29 2015 Well, it is an already existing C library I am only adapting. And
- Idan Arye (5/35) Apr 30 2015 If you invest some effort in redefining the event types as D
- weaselcat (5/15) Apr 29 2015 I think the article highlights the issue with the current
Hello there, I took the occasion of the D hackathon to finally write a technical article. I am programming games as a hobby and find the D programming language perfectly suited for this task. So I thought I could write an article about how I use D's capabilities to program games efficiently. So here it is: https://marfisc.wordpress.com/2015/04/29/using-d-templates-for-gamedev/ What do you think? Any remarks? Mafi
Apr 29 2015
On 4/29/15 8:26 AM, Mafi wrote:Hello there, I took the occasion of the D hackathon to finally write a technical article. I am programming games as a hobby and find the D programming language perfectly suited for this task. So I thought I could write an article about how I use D's capabilities to program games efficiently. So here it is: https://marfisc.wordpress.com/2015/04/29/using-d-templates-for-gamedev/ What do you think? Any remarks? Mafihttps://www.reddit.com/r/programming/comments/34ah1q/using_d_templates_for_game_development/ https://news.ycombinator.com/newest https://twitter.com/D_Programming/status/593450243942940672 https://www.facebook.com/dlang.org/posts/1059784987368515 Andrei
Apr 29 2015
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... switch(polledEvent.type) { mixin(caseOnEvent!("SDL_QUIT", "quit")); mixin(caseOnEvent!("SDL_ACTIVEEVEENT", "active")); mixin(caseOnEvent!("SDL_KEYDOWN", "eventKeyDown")); // (*) mixin(caseOnEvent!("SDL_KEYUP", "eventKeyUp")); // (*) mixin(caseOnEvent!("SDL_MOUSEMOTION", "motion")); mixin(caseOnEvent!("SDL_MOUSEBUTTONUP", "eventMouseButtonUp")); // (*) mixin(caseOnEvent!("SDL_MOUSEBUTTONDOWN", "eventMouseButtonDown")); // (*) mixin(caseOnEvent!("SDL_JOYAXISMOTION", "jaxis")); mixin(caseOnEvent!("SDL_JOYBALLMOTION", "jball")); mixin(caseOnEvent!("SDL_JOYHATMOTION", "jhat")); mixin(caseOnEvent!("SDL_JOYBUTTONDOWN", "eventJoyButtonDown")); // (*) mixin(caseOnEvent!("SDL_JOYBUTTONUP", "eventJoyButtonUp")); // (*) mixin(caseOnEvent!("SDL_USEREVENT", "user")); mixin(caseOnEvent!("SDL_SYSWMEVENT", "syswm")); default: //has to be there even if empty static if(is(typeof(that.onOther(Event.init)))) { that.onOther(polledEvent); break; } } 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
Apr 29 2015
On Wednesday, 29 April 2015 at 16:55:58 UTC, bearophile wrote:Mafi:Well, it is an already existing C library I am only adapting. And additionally the shonwn template encapsulates the whole outer loop as well.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...[...] 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, bearophileI 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.
Apr 29 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: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/1e992d248355Mafi:Well, it is an already existing C library I am only adapting. And additionally the shonwn template encapsulates the whole outer loop as well.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...[...] 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, bearophileI 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.
Apr 30 2015
On Wednesday, 29 April 2015 at 15:26:58 UTC, Mafi wrote:Hello there, I took the occasion of the D hackathon to finally write a technical article. I am programming games as a hobby and find the D programming language perfectly suited for this task. So I thought I could write an article about how I use D's capabilities to program games efficiently. So here it is: https://marfisc.wordpress.com/2015/04/29/using-d-templates-for-gamedev/ What do you think? Any remarks? MafiI think the article highlights the issue with the current std.variant implementation, lack of pattern matching/destructuring in D, and need for better tuple syntax. Nice article
Apr 29 2015