Thread overview
Assign Range: layout = X, AlignRight;
May 26, 2020
WebFreak001
May 26, 2020
Paul Backus
May 26, 2020
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!

May 26, 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!

Main goal is: Readable, clean, beauty code.

May 26, 2020
On Tuesday, 26 May 2020 at 13:36:34 UTC, Виталий Фадеев wrote:
> [...]
>
> I want this feature in D!

I think you are rather looking for tuples:

         void opAssign(Args...)(Tuple!Args args)
         {
             foreach( a; args )
             {
                 _layouts ~= a;
             }
         }

which you can currently use like

    layout = tuple(X, AlignRight);

which is probably going to be the best you can do with D right now. You can also make your own type like Tuple and/or a function like tuple if you want more type-safety



(and maybe some day in the distant future once the comma operator is completely removed and a DIP for tuple syntax goes through we might even be able to write layout = (X, AlignRight))
May 26, 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");