DMD 1.035 and 2.019 releases

Jarrett Billingsley jarrett.billingsley at gmail.com
Mon Sep 8 12:12:36 PDT 2008


On Mon, Sep 8, 2008 at 9:39 AM, Bruno Medeiros
<brunodomedeiros+spam at com.gmail> wrote:
> Jarrett Billingsley wrote:
>>
>> Now were you just reading off of
>> http://d.puremagic.com/issues/show_bug.cgi?id=2170 or did you come to
>> this conclusion independently ;)
>
> I was reading that, and I must say, I didn't quite understand the reasoning
> behind that named function parameters proposal.

Do you mean you don't understand the need for named function
parameters, or you're not clear on the mechanism?

If the former, named parameters are just a clearer way of calling
functions that take a lot of parameters or a lot of potentially
confusing parameters.  If the latter, D currently allows you to do
something like this:

class A
{
   this(int, int, int){}
}

void foo(A a...)
{
   // a is a reference to a new instance of A
}

foo(1, 2, 3); // same as foo(new A(1, 2, 3));

So my idea is, if struct literals allow for named members, it's not
much of a step to go from

foo(SomeStruct{x: 1, y: 2})

to:

foo(x: 1, y: 2)

> And what do you mean with "The issue with this is that you have to have a
> different named struct for every function that takes this style of
> parameters" ?
> Were you suggesting that functions who want to receive named parameters
> should declare an arguments struct just for that?

Yes.  Although without the syntactic sugar, you'd end up having to write i.e.

foo(FooArgs{x: 1, y: 2})
bar(BarArgs{z: 3.4, w: "hi"})

hence having to repeat the name of the function once for the function
itself and again for the named arguments.  With sugar, the argument
structure name and braces go away, but it still doesn't solve the
problem of having to declare an argument structure for every function.

But, if we were to get named parameters for all functions, then both
problems are solved: you have named parameters for free functions and
methods, and you can initialize structs that have constructors or
static opCalls with named members instead of having to write them in
order:

struct S
{
    int x, y, z;
    this(int x, int y, int z) { this.x = x; this.y = y; this.z = z; }
}

S(1, 2, 3) and S(x: 1, y: 2, z: 3) are now both legal.


More information about the Digitalmars-d-announce mailing list