A pattern I'd like to see more of - Parsing template parameter tuples

Sjoerd Nijboer sjoerdnijboer at gmail.com
Mon May 21 15:01:23 UTC 2018


On Monday, 21 May 2018 at 14:36:32 UTC, Jacob Carlborg wrote:
> enum Options options = { foo: true, bar: false, a: 42, b:  
> "guess what this does" };
> SomeObject!options o;
>
> --
> /Jacob Carlborg

I like this especially if you mix it with:


enum Options options = { foo: true, bar: false, a: 42, b: "guess 
what this does" };
SomeObject!options o;

class myClass(Options options)
{
     void myFunction()
     {
         static if(options.foo)
         {
             ... //some code
         }
     }
}

class MyOtherClass(Options options)
{
     void myOtherFunction()
     {
         static if(options.foo && options.bar)
         {
             ... //some code
         }
     }
}

All of a sudden you encaptulate your template arguments whitout a 
lot of effort and allow them to be easily re-used, computed and 
altered.
It becomes even more convenient if you're computing the content 
of `Options` at compile time using some complicated statements.



More information about the Digitalmars-d mailing list