named parameters to functions

Bill Baxter dnewsgroup at billbaxter.com
Thu Oct 11 16:41:56 PDT 2007


BCS wrote:
> Reply to Zola,
> 
>> Hi,
>>
>> I'm amazed by some things D does have,
>> Except I'm puzzled by this one one gaping hole in D.
>> It's something I've been pissed off about C++ for 10 years now: Named
>> parameters to functions.
>>
>> window* w = new_window("alert box", movable=false);
>>
>> We can almost do this in C++ today with boost.parameter and I hate it
>> so much that I refuse to use it, because the only way to do so is by
>> polluting your namespace.
>>
>> I'm really surprised to see that D doesn't have this super important
>> feature. It redefines and simplifies a whole world of class design..
>> for user interfaces especially.
>>
>> If D included this, I think it would be the tipping point to really
>> getting me into D, and making a serious library for it.
>>
>> Please.
>>
>> - Z
>>
> 
> It's have been proposed and favorably received. It's now on the list of 
> things to do at some point in the not to distant future.

Favorably received by whom?  I recall lengthy discussions -- I started 
one of them with a post remarkably like Zola's as one of my first posts 
to the D newsgroup -- but I don't recall any favorable reception except 
by other D users.   And the fact that it didn't make it into the 
WalterAndrei.pdf makes me think that if D gets it at all it will be 
after all that stuff, so methinks it will be "distant future" at best.

> For now you can sort of fake it with struct literals.

Not really.  Struct literals 
(http://www.digitalmars.com/d/1.0/struct.html#StructLiteral) don't give 
you pick-and-choose initialization.  You can't just override the default 
value of the 5th item, for instance.  So in that respect they work just 
like regular function parameter defaults.

Static struct initializers, on the other hand, have the right look, 
x={name:"fred", title:"director"}, but since they only work with 
constants they're not much practical use either.

So you're left with creating a struct explicitly and passing it.

func_args x;  x.name = "fred"; x.title="director";
func(x);  // for func(func_args a);
or
func(x.tupleof); // for func that takes list of args that matches with 
func_args.

Either way it's more trouble than it's worth.

Were you thinking of something else?

--bb



More information about the Digitalmars-d mailing list