Unexpectedly nice case of auto return type

mipri mipri at minimaltype.com
Tue Dec 3 10:13:30 UTC 2019


On Tuesday, 3 December 2019 at 10:02:47 UTC, Andrea Fontana wrote:
> On Tuesday, 3 December 2019 at 09:48:39 UTC, Basile B. wrote:
>> You see what surprises me here is that we cannot express the 
>> special type that is `TypeNull` and that can only have one 
>> value (`null`) so instead we have to use `auto` or 
>> `typeof(null)`.
>
> You can still create an alias anyway :)
>
> alias TypeNull = typeof(null);

Speaking of nice stuff and aliases, suppose you want to
return a nice tuple with named elements?

Option 1: auto

   auto option1() {
       return tuple!(int, "apples", int, "oranges")(1, 2);
   }

Option 2: redundancy

   Tuple!(int, "apples", int, "oranges") option2() {
       return tuple!(int, "apples", int, "oranges")(1, 2);
   }

Option 3: an alias

   alias BadMath = Tuple!(int, "apples", int, "oranges");

   BadMath option3() {
       return BadMath(1, 2);
   }

It wasn't obvious to me that BadMath(...) would work.
It *should* be obvious since this also works:

   ...
       return Tuple!(int, "apples", int, "oranges")(1, 2);

But the convenience of tuple() helped to  mask that.

I was going with silly stuff like

   ...
       return cast(BadMath) tuple(1, 2);



More information about the Digitalmars-d-learn mailing list