returning different types via function

Meta jared771 at gmail.com
Tue Nov 19 20:41:17 PST 2013


On Tuesday, 19 November 2013 at 19:31:41 UTC, bearophile wrote:
> The clean design is to use std.typecons.Nullable:
>
> Nullable!(string[]) func(string[] zz) {

I have found that Nullable is pretty much useless for this type 
of usage, because it aliases itself to the underlying value, and 
then any safety it would provide is lost. See the following:

import std.typecons;

Nullable!(string[]) func(string[] zz) pure nothrow
{
	return Nullable!(string[])();
}

void main()
{
         //AssertError thrown for trying to get
         //a value that is null. Might as well
         //return null at this point
	auto x = func(["test"]) ~ ["test"];
}

Nullable either needs to be changed, or a new type that doesn't 
alias itself to the underlying value needs to be added to 
std.typecons.


More information about the Digitalmars-d-learn mailing list