digitalmars.D.learn - A tutorial on D templates
- Philippe Sigaud <philippe.sigaud gmail.com> Jan 13 2012
- "Joel" <joelcnz gmail.com> Jan 14 2012
- =?utf-8?Q?Simen_Kj=C3=A6r=C3=A5s?= <simen.kjaras gmail.com> Jan 16 2012
[Cross-posted with D.announce, since it's also an annoucement] Hello all, I discovered D a few years ago and, seeing the recent increase in community projects, I looked for a way to bring my own small part to it. I quite like D templates and wanted to try LaTeX again, so I decided to bite the bullet and wrote a tutorial on templates. It's far from finished and most probably full of mistakes but since it's already quite big, I need some inputs. It's a Github project, here: https://github.com/PhilippeSigaud/D-templates-tutorial The resulting pdf is there: https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/dtemplates.pdf (click on View Raw) If you have any comment, criticism, explanation, what have you, I'm game. What section should be expanded, what example would be cool, etc. If you see a mistake, do not hesitate to tell me: it's the first time I put thoughts on paper like this. Github issues management is far from perfect, but it's usable. Even better would be pull requests :) There is an 'Examples' section where I show what can be done with templates and there I 'borrowed' some code posted here, with attribution. I already exchanged with Andrej Mitrovic (thanks!), but also took some code from Timon Gehr, Simen Kjaeraas, Trass3r and Jacob Carlborg. Guys, if any of you have a problem with that, tell me so and I'll take down the code of course. But if any of you could give me some explanation (a small paragraph or two?) about what your code does, I'll be forever grateful :) This also extend to anyone who would want to share some template love/lore with the rest of us. Philippe
Jan 13 2012
On Fri, 13 Jan 2012 22:21:52 +0100, Philippe Sigaud <philippe.sigaud gmail.com> wrote:There is an 'Examples' section where I show what can be done with templates and there I 'borrowed' some code posted here, with attribution. I already exchanged with Andrej Mitrovic (thanks!), but also took some code from Timon Gehr, Simen Kjaeraas, Trass3r and Jacob Carlborg. Guys, if any of you have a problem with that, tell me so and I'll take down the code of course. But if any of you could give me some explanation (a small paragraph or two?) about what your code does, I'll be forever grateful :)
The extended enum example does not compile, because you've removed the unittest{} block around the the tests. I'd say the code in your document should compile straight out of the box, to be newb-friendly. As for an explanation: string EnumDefAsString(T)() if (is(T == enum)) { string result = ""; foreach (e; __traits(allMembers, T)) result ~= e ~ " = T." ~ e ~ ","; return result; } This piece of code iterates over all members of the passed enum T, generating a string containing all members and their values. For this enum: enum bar { a, b, c } the generated string looks like this (if you want to check this, feel free to call EnumDefAsString at run-time and print its result): "a = bar.a,b = bar.b,c = bar.c" As we can see, this is a valid body for an enum. That means we can use mixin() to generate this exact same enum. But wait - there's more: template ExtendEnum(T, string s) if (is(T == enum) && is(typeof({mixin("enum a{"~s~"}");}))) { mixin( "enum ExtendEnum {" ~ EnumDefAsString!T() ~ s ~ "}"); } This code concatenates the string generated from the previous function with that passed to the function as parameter s. So with bar previously defined, and this instantiation: ExtendEnum!(bar, "d=25") the body of the function will look like this (after string expansion): mixin( "enum ExtendEnum {" ~ "a = bar.a,b = bar.b,c = bar.c" ~ "d=25" ~ "}"); concatenating those strings, we see that we have a valid enum definition: enum ExtendEnum {a = bar.a,b = bar.b,c = bar.c,d=25} The mixin then pastes it in, and it is compiled as regular D code. TLDR: This code generates an enum definition as a string, by taking all the members of the old enum, and adding those passed in string parameter s, and mixing it in.
Jan 16 2012
On Mon, Jan 16, 2012 at 17:36, Simen Kj=C3=A6r=C3=A5s <simen.kjaras gmail.c= om> wrote:The extended enum example does not compile, because you've removed the unittest{} block around the the tests. I'd say the code in your document should compile straight out of the box, to be newb-friendly.
Yeah. I just coded a small D script that extract all code samples, compile them and store the result in a log. That way, I can now slowly make all samples compile. I hesitate between showing the boilerplate or not (imports, empty mains or code in main...)As for an explanation:
This code generates an enum definition as a string, by taking all the members of the old enum, and adding those passed in string parameter s, and mixing it in.
Thanks! I'll add it.
Jan 16 2012
On Tue, 17 Jan 2012 00:10:41 +0100, Philippe Sigaud = <philippe.sigaud gmail.com> wrote:On Mon, Jan 16, 2012 at 17:36, Simen Kj=C3=A6r=C3=A5s <simen.kjaras gm=
wrote:The extended enum example does not compile, because you've removed th=
unittest{} block around the the tests. I'd say the code in your docum=
should compile straight out of the box, to be newb-friendly.
Yeah. I just coded a small D script that extract all code samples, compile them and store the result in a log. That way, I can now slowly make all samples compile. I hesitate between showing the boilerplate or not (imports, empty mains or code in main...)
I would say it should be there. If possible, perhaps have those pieces a= ll gray, to mark them as unimportant?
Jan 17 2012









"Joel" <joelcnz gmail.com> 