Idioms you use

Marco Leise via Digitalmars-d digitalmars-d at puremagic.com
Sat Oct 3 06:53:35 PDT 2015


Am Mon, 28 Sep 2015 21:40:43 +0000
schrieb Freddy <Hexagonalstar64 at gmail.com>:

> 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



More information about the Digitalmars-d mailing list