TIL: auto struct members
    Basile B. via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Tue Oct 18 20:30:06 PDT 2016
    
    
  
On Tuesday, 18 October 2016 at 22:12:47 UTC, Ali Çehreli wrote:
> 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)
It also works if it's an enum, but without surprise because this 
kind of enums are grammatically the same as an auto declaration.
    
    
More information about the Digitalmars-d-learn
mailing list