Created
February 21, 2026 23:07
-
-
Save crazymonkyyy/b748c4ed79c63b8b9bd72ac8e2f5754c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!opend test app.d | |
| --- app.d | |
| import std; | |
| mixin template makeid(string[][] fields,alias INT=ubyte){ | |
| static assert(INT.min==0, "I assume unsigned"); | |
| enum string enumsmixin=(){ | |
| string[] o; | |
| o~="enum allids{nil=INT(0),"; | |
| INT i=INT(1); | |
| foreach(list;fields){ | |
| o~="enum INT actaulmin(T:"~list.front~")="~i.to!string~";"; | |
| o~="enum "~list.front~"{nil=INT(0),"; | |
| list.popFront; | |
| foreach(s;list){ | |
| o[0]~=s~"=INT("~i.to!string~"),"; | |
| o[$-1]~=s~"=INT("~i.to!string~"),"; | |
| i++; | |
| } | |
| o[$-1]~="}"; | |
| } | |
| o[0]~="}"; | |
| return o.join('\n'); | |
| }(); | |
| enum INT nil=INT(0); | |
| mixin(enumsmixin); | |
| enum string[] idnames=["nil"]~fields.map!(a=>a.drop(1)).join.array; | |
| } | |
| version(unittest){ | |
| mixin makeid!([ | |
| ["wallsid","wall","ground","fakewall"], | |
| ["actorsid","player","goblin","horse"], | |
| ["weponsid","ax","sword"], | |
| ]); | |
| unittest{ | |
| //enumsmixin.writeln; | |
| 6.to!allids.writeln; | |
| assert(cast(allids)weponsid.ax==allids.ax); | |
| idnames.writeln; | |
| idnames[weponsid.ax].writeln; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment