Optional extra return value? Multiple return values with auto?

ReneSac reneduani at yahoo.com.br
Mon Jul 23 20:25:54 PDT 2012


How I can return multiple values in D, but one of them being 
optional? I tried the 'out' hack to achieve multiple return 
values, but it didn't accepted a default argument: it needed a 
lvalue in the calling function.

In Lua, for example, one can do:

function foo(input)

     -- calculations --

     return result, iterations_needed
end

a, stats = foo()

or, if you are only interested in the result:

a = foo() -- the second return is automatically discarded.

Do I really have to duplicate the function, in order to achieve 
this?

Also, is there some way to use auto with multiple returns? Like:

bool bar(out ulong output){ output = 0 ; return true;}

auto check = bar(auto num); // error

Right now, I need to declarate num outside the function, and thus 
I can't use the auto keyword.


More information about the Digitalmars-d-learn mailing list