shorter alternative of constructor with parameter

simendsjo via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 23 00:44:59 PDT 2014


On 06/22/2014 09:58 PM, Kapps wrote:
> On Sunday, 22 June 2014 at 11:50:31 UTC, Shammah Chancellor wrote:
>> I can't support this proposal.   Adds more syntax to a language that
>> is already becoming cramped.  I also don't see the purpose of having
>> simple constructors like this?  Are you going to add (n choose k)
>> simple constructors to a class?   I could get behind field initializer
>> syntax ala C# "with" modeling the syntax we have for structures.
>>
>> new Person() {name: "Bob"};
>>
>> -Shammah
> 
> In theory, 'with' could be used for that if it returned the expression
> passed in:
> auto a = with(new Person()) {
>     Name = "Bob";
>     Age = 27;
> }

I had to test that syntax. I got it working with these versions:
    C c; with(c = new C()) {
        i = 10;
    }

    C d = new C(); with(d) {
        i = 20;
    }

I really like the C# object initializer syntax, so your version would be
much appreciated. Or if with could create objects in outer scope like this

    with(auto c = new C()) {
        i = 30;
    }



More information about the Digitalmars-d mailing list