Optional extra return value? Multiple return values with auto?

bearophile bearophileHUGS at lycos.com
Tue Jul 24 03:33:40 PDT 2012


On Tuesday, 24 July 2012 at 03:25:55 UTC, ReneSac wrote:
> How I can return multiple values in D, but one of them being 
> optional?

One of the ways to to it is to return a tuple with your 
arguments, where the last item of the tuple is a Nullable of the 
optional element:


import std.stdio, std.typecons;

Tuple!(int, double, Nullable!int) foo(bool b) {
     if (b)
         return tuple(5, 1.5, Nullable!int(1));
     else
         return tuple(10, 2.5, Nullable!int());
}

void main() {
     writeln(foo(false)[2]); // enforcement failed
}


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list