out default argument of void
Simen Kjærås
simen.kjaras at gmail.com
Wed Jan 4 15:02:19 PST 2012
On Wed, 04 Jan 2012 23:19:18 +0100, Caligo <iteronvexor at gmail.com> wrote:
> I have a function that looks something like this:
>
> bool fun(double theta, out A a, out B b, out C c){ /* ... */ }
>
> if fun() returns false, then nothing is supposed to be assigned to a,
> b, c. If it returns true, then values are assigned to a, b, c. Also,
> there are two ways to call fun(): If I'm interested in the return
> value only, then
>
> 1. fun(theta);
>
> otherwise,
>
> 2. fun(theta, a, b, c);
>
> Obviously, method #1 won't work because there is no such thing as:
>
> bool fun(double theta, out A a = void, out B b = void, out C c =
> void){ /* ... */ }
>
> is there?
>
> So, I have to lose 'out' and use pointers instead:
>
> bool fun(double theta, A* a = null, B* b = null, C* c = null){ /* ...
> */ }
>
> I don't want to use a variadic function for this either because it's
> not really a variadic function.
>
> 1. Are there any other solutions ?
A few days ago, I posted this code:
template LRef( T, string f = __FILE__, int l = __LINE__ ) {
static T LRef;
}
ref T lref( T, string f = __FILE__, int l = __LINE__ )( T value ) {
LRef!( T, f, l ) = value;
return LRef!( T, f, l );
}
unittest {
assert( __traits( compiles, lref(0) = 3 ) );
}
It would allow you to have a throwaway value to use for default
parameters:
void foo( ref int n = lref(0) ) {
}
Also, note that I'm using ref, not out. out parameters are initialized
to their default value upon entering the function, so would not give
the behavior you want.
> 2. Would it make sense to have 'out default argument of void' in D?
No. ref and out are basically non-nullable pointers, and having them
nullable opens a whole barrel of worms.
More information about the Digitalmars-d-learn
mailing list