January 16, 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.
January 16, 2012
On Mon, Jan 16, 2012 at 17:36, Simen Kjærås <simen.kjaras@gmail.com> 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.
January 17, 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ærås <simen.kjaras@gmail.com> 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...)

I would say it should be there. If possible, perhaps have those pieces all
gray, to mark them as unimportant?
January 31, 2012
On 01/14/2012 07:49 AM, Philippe Sigaud wrote:

> That's cool, because the D community is small enough and still
> dispersed enough that we should link one another to bring it all
> together.

Thank you very much for your link! :)

And I've finally found time to create two links to your 'D Templates: A Tutorial' from the following page

  http://ddili.org/ders/d.en/templates.html

Ali

P.S. Going off-topic, the following chapters of 'Programming in D' have been translated since the last announcement:

* Redirecting Standard Input and Output Streams
* Files
* auto and typeof
* Name Space
* The for Loop
* The Ternary Operator ?:
* Literals
* Formatted Output

Now the book can also be downloaded as pdf from the [Download as PDF] link on chapter headers:

  http://ddili.org/ders/d.en/index.html

January 31, 2012
On Tue, Jan 31, 2012 at 09:12, Ali Çehreli <acehreli@yahoo.com> wrote:
> On 01/14/2012 07:49 AM, Philippe Sigaud wrote:
>
>> That's cool, because the D community is small enough and still dispersed enough that we should link one another to bring it all together.
>
> Thank you very much for your link! :)

That's quite normal.

> And I've finally found time to create two links to your 'D Templates: A Tutorial' from the following page
>
>  http://ddili.org/ders/d.en/templates.html

And thank you too!

> Ali
>
> P.S. Going off-topic, the following chapters of 'Programming in D' have been translated since the last announcement:
>
> * Redirecting Standard Input and Output Streams
> * Files
> * auto and typeof
> * Name Space
> * The for Loop
> * The Ternary Operator ?:
> * Literals
> * Formatted Output
>
> Now the book can also be downloaded as pdf from the [Download as PDF] link on chapter headers:
>
>  http://ddili.org/ders/d.en/index.html

I think you should make an independent announce on D.announce and D.learn. And maybe then monthly (or every three month, say) updates on the state of your book.
1 2
Next ›   Last »