www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Assign Range: layout = X, AlignRight;

reply =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
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
next sibling parent =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
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
prev sibling next sibling parent WebFreak001 <d.forum webfreak.org> writes:
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
prev sibling parent Paul Backus <snarwin gmail.com> writes:
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");
May 26 2020