TIL: auto struct members

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Oct 18 15:12:47 PDT 2016


It may be embarrassing to discover this fact so late but you can define 
struct members as 'auto':

import std.range;
import std.algorithm;

struct S {
     auto r = only("a", "b").cycle;    // <-- WOW!
}

pragma(msg, typeof(S.r));
/* Prints:
  *     Cycle!(OnlyResult!(string, 2LU))
  */

// It's extra cool that S and the whole construct is @nogc pure nothrow
// (In that regard, only() is better than an array as the latter cannot
// be @nogc. i.e. [ "a", "b", "a" ] cannot be @nogc.)
void foo() @nogc pure nothrow {
     assert(S().r.take(3).equal(only("a", "b", "a")));
}

void main() {
}

Ali

P.S. I propose a new attribute, @cool, which should mean '@nogc pure 
nothrow'. :o)


More information about the Digitalmars-d-learn mailing list