Tuples citizenship

deadalnix deadalnix at gmail.com
Thu Mar 1 15:05:22 PST 2012


Le 01/03/2012 23:33, Jonathan M Davis a écrit :
> On Thursday, March 01, 2012 17:08:44 bearophile wrote:
>> I think std.typecons.Tuples merit to be a little more citizens in D and
>> Phobos. I think reducing the usage of "out" argument, and replacing them
>> with a tuple result, will avoid mistakes and make the code look better. In
>> std.math there are functions that maybe are better to use
>> std.typecons.Tuple:
>>
>> pure nothrow @trusted real frexp(real value, out int exp);
>> ==>
>> pure nothrow @trusted Tuple!(real, int) frexp(real value);
> [snip]
>
> Having good tuple support is great, but out parameters are great too. I'm sure
> that there are plenty of cases where using out parameters is actually far
> cleaner than using tuples, since you don't have multiple return values to deal
> with. So, better enabling tuples is great, but I don't think that we're
> necessarily moving in a good direction if we're trying to eliminate out
> parameters in favor of tuples.
>
> - Jonathan M Davis

I don't think out parameter is a great idea. This is rather confusing.

I tend to think as function's parameter as input of the function and 
return value as an output. Books usualy agree, so I guess it is a valid 
point.

OOP give us another parameter to play with : this. It act as a state 
that can be modified. UFCS is nice to extends that outside OOP. This is 
a way better alternative than out parameter.

Tuples are nice too. Since we have auto, this isn't a big deal.

Both should be preferred to out parameters IMO, because the later cannot 
be differentiated at the calling point and force the programer to refers 
to the function declaration all the time or use its - limited and 
sometime inaccurate - memory. This is something we want to avoid.


More information about the Digitalmars-d mailing list