Assign Range: layout = X, AlignRight;

Paul Backus snarwin at gmail.com
Tue May 26 14:15:34 UTC 2020


On Tuesday, 26 May 2020 at 13:36:34 UTC, Виталий Фадеев wrote:
> I want this:
>
>     layout = X, AlignRight;
>
>
>
> Use case:
> class Widget
> {
>     struct Layout
>     {
>         ILayout[] _layouts;
>
>         void opAssign( args... )
>         {
>             foreach( a; args )
>             {
>                 _layouts ~= a;
>             }
>         }
>
>
>         alias _layouts this;
>     }
> }
>
>
> in front-end code:
>
>     with( new Widget() )
>     {
>         layout = X, AlignRight;
>     }
>
>
> I think the solution:
>    void opAssign( args... ) { ... }
>
>
>
> I want this feature in D!

You can do this by wrapping your argument list with the template 
`std.meta.AliasSeq`:

import std.stdio: writeln;
import std.meta: AliasSeq;

struct Example
{
     void opAssign(Args...)(Args args)
     {
         writeln("called with ", Args.stringof);
     }
}

void main()
{
     Example e;
     e = AliasSeq!(1, 2, "hello");
     // prints: called with (int, int, string)
}

I believe there is a proposal in the works to make it possible to 
do this without AliasSeq, but I'm not sure how far along it is. 
For now, if you want to make the code prettier, I recommend using 
an alias to give AliasSeq a less ugly name. For example:

     alias Args = AliasSeq;
     e = Args!(1, 2, "hello");


More information about the Digitalmars-d-learn mailing list