Am Mon, 28 Sep 2015 21:40:43 +0000
schrieb Freddy <[email protected]>:

> Are any D idioms you use that you like to share?
> Heres one of mine
> ---
> enum ctfe =
> {
>      return 0xdead & 0xbad;
> }();
> ---

Yep, using that often, although I try to get my head around
using functional style one-line expressions where possible to
avoid the boilerplate.

static immutable ctfe = {
    bool[256] result;
    foreach (i; 0 .. 256)
        result = isDigit(i);
    return result;
}();

==>

static immutable bool[256] ctfe = iota(256).map!isDigit.array;

==>

static ctfe = ctfeArr!( iota(256).map!isDigit );

enum ctfeArr(alias r)()
{
        // r.length doesn't work as static array size
        enum length = r.length;
        // immutable doesn't work on this (cannot modify const)
        ElementType!(typeof(r))[length] result = r.array;
        // Cross fingers here ...
        return cast(immutable) result;
}

-- 
Marco

Reply via email to