Struct initializers as expressions

Marc Schütz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Dec 4 02:42:46 PST 2015


On Thursday, 3 December 2015 at 15:31:49 UTC, Chris Wright wrote:
> On Thu, 03 Dec 2015 06:38:20 +0000, Mike Parker wrote:
>
>> AFAIK, your only option is to use a struct constructor. This 
>> is the sort of thing they're  used for.
>
> Which brings be back to positional arguments, which means that 
> someone wishing to supply a limit on the number of query 
> results must also give me the number of results per page, even 
> if she doesn't care about the number of results per page.
>
> Is it worth posting a DIP for struct literals of the form 
> `Name{fieldname: value}` ?

I'd support that, too.

I suggest to make the struct name optional:

     struct S { int a, b; }
     struct T { string a, b; }
     void foo(S s);
     void foo(T t);

     foo({b: 1, a: 2});  // foo(S(2, 1));
     foo({a: "bla"});    // foo(T("bla", null));

Then we can add some syntax sugar to leave out the braces, too:

     void bar(int a, T t)
     bar(42, a: "bla", b: "xyz");

This effectively gives us strongly typed named arguments, without 
making the names part of the function signature, which Walter 
objected to the last time something like this was proposed.


More information about the Digitalmars-d-learn mailing list