Voldemort declarations inside structs with ctor initialization

via Digitalmars-d digitalmars-d at puremagic.com
Tue May 27 07:55:32 PDT 2014


Considering something like this:

     auto stuff = [1, 2, 3];

     struct Foo
     {
         typeof(stuff.filter!("a != 2")) foo;

         this(int = 0)
         {
             // assume we must initialize foo in ctor,
             // for instance because it needs ctor args;
             // in this contrived case because 'stuff'
             // is not known at compile-time
             foo = stuff.filter!("a != 2");
         }

         auto front() { return foo.front(); }
         void popFront() { return foo.popFront(); }
         bool empty() { return foo.empty; }
     }

If the language allowed foo to be declared using auto (which 
would be deduced from the assignment in the ctor), that would be 
nice, right? Is that too hard to implement? As it stands, in some 
situations the declaration can get rather awkward and unwieldy 
(yes, an alias helps). It also violates the DRY principle, 
requiring changes in two places, when they do occur.

BTW, why doesn't this example work with lambdas (a => a != 2) 
instead of a string mixin ("a != 2")?

BTW 2, is `this(int = 0)' the best workaround (still?) for the 
fact that I want the ctor to be called, even if it doesn't really 
require any parameter?


More information about the Digitalmars-d mailing list