Idioms you use
Artur Skawina via Digitalmars-d
digitalmars-d at puremagic.com
Sat Oct 3 15:08:39 PDT 2015
On 10/03/15 15:53, Marco Leise via Digitalmars-d wrote:
> 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;
> }
==>
static ctfe = ctfeArr!( iota(256).map!isDigit() );
immutable typeof(R.front)[R.array().length] ctfeArr(alias R) = R.array();
[`array` is only required because of compiler issues, yes.]
artur
More information about the Digitalmars-d
mailing list