Flag proposal

Nick Sabalausky a at a.a
Fri Jun 10 12:47:20 PDT 2011


"Michel Fortin" <michel.fortin at michelf.com> wrote in message 
news:istqjn$2jld$1 at digitalmars.com...
> On 2011-06-10 14:52:53 -0400, Robert Clipsham <robert at octarineparrot.com> 
> said:
>
>> On 10/06/2011 19:06, Andrei Alexandrescu wrote:
>>> Ugly is in the eye of the beholder, but I fail to see how the added
>>> punctuation makes Flag!"param".yes significantly more verbose than param
>>> : true.
>>
>> foo(param: true, otherParam: false);
>> foo(Flag!"param".yes, Flag!"otherParam".no);
>>
>> I don't know about you, but I find the former is far more legible. I'd 
>> hate to see my code littered with Flag!"something".
>
> I have to say I totally agree with Robert.
>
> I agree with the need for a way to name parameters, but instantiating a 
> pseudo-boolean type from a template for each function parameter is worse 
> than the initial problem. Templates are already a difficult concept to 
> grasp for most people (because they're so abstract), we really don't need 
> to replace every boolean parameter with a template type.
>
>
>>> The problem that named parameters are still optional remains. Or we need
>>> to add one extra language feature to specify required named parameters.
>>
>> void foo(bool param, bool otherParam, bool thisOneIsntRequired = false);
>>
>> Call it with or without named parameters, two are required, one is not.
>>
>> foo(otherParam: true, param: false);
>> foo(true, false);
>> foo(otherParam: true, param: false, thisOneIsntRequired: true);
>
> And just try to think of the signature for the function above if it was 
> using Flag!
>
> void foo(Flag!"param" param, Flag!"otherParam" otherParam, 
> Flag!"thisOneIsntRequired" thisOneIsntRequired = 
> Flag!"thisOneIsntRequired".no);
>
> Do we really want to expose that in the documentation?
>

I completely agree with Robert and Michel on all the points they've raised. 
Flag is admittedly a great example of the power of D's templates. And it's 
admittedly a very clever hack to get around *some* of the limitations of not 
having named parameters...*But* it's *still* just that: a hack to get around 
some of the limitations of not having named parameters. And yes, it is 
comparatively ugly, as Robert and Michel's examples have very clearly shown.

Also:

>>> The problem that named parameters are still optional remains. Or we need
>>> to add one extra language feature to specify required named parameters.

If by that, you meant that the caller is allowed to call the function 
without naming the parameters, I really don't see that as a problem. Yea, 
*maybe* it would be better if the callee could optionally decide "caller 
must use named params", but even without that, it's a hell of a lot better 
than the current state, and it's also a lot better than Flag becase 1. It's 
*much* cleaner for both the caller and callee, and 2. It works for functions 
like foo(int, int, int, int), not just bools.





More information about the Digitalmars-d mailing list