Optional extra return value? Multiple return values with auto?

ReneSac reneduani at yahoo.com.br
Sat Aug 11 15:48:33 PDT 2012


On Tuesday, 24 July 2012 at 05:30:49 UTC, Ali Çehreli wrote:
> The options that I can think of:
>
> - Return a struct (or a class) where one of the members is not 
> filled-in
>
> - Similarly, return a tuple

This is awkward, and doesn't look good for performance.

>
> - Use an out parameter, which can have a default lvalue:
>
> int g_default_param;
>
> void foo(ref int i = g_default_param)
> {
>     if (&i == &g_param) {
>         // The caller is not interested in 'i'
>
>     } else {
>         // The caller wants 'i'
>         i = 42;
>     }
> }
>
> void main()
> {
>     foo();
>
>     int i;
>     foo(i);
>     assert(i == 42);
> }

This is not working inside a class. I'm not sure what default 
value I should put when I don't know the type entered:

class a (T) {
	T dummy = T.init;
	bool foo(int a, out T optional = dummy)
	{
		return true;
	}
}

void main () {
	auto c = new a!uint();
	c.foo(5);
}

I get the following error:

Error: need 'this' to access member dummy



More information about the Digitalmars-d-learn mailing list