Null references
Nick Treleaven
nospam at example.net
Thu Aug 23 09:27:41 PDT 2012
On 21/08/2012 21:17, Nick Treleaven wrote:
>> I have an Option struct on my home computer that only allows access to
>> the held value if the error case is simultaneously handled:
>>
>> Option!int a = foo();
>>
>> int n = a.match!(
>> (int x) => x,
>> (None n) => 0 // or throw, or what have you.
>> );
I've now implemented match, similar to the above:
https://github.com/ntrel/d-maybe/blob/master/maybe.d#L222
My match() always returns Maybe!T, because for pointers the lambdas
could return null.
Taken from the unittest:
assert(match!(to!string, ()=>"<invalid>")(maybe(2)) == "2");
assert(match!(to!string, ()=>"<invalid>")(Maybe!int()) == "<invalid>");
assert(match!((x, y)=>text(x, y), {})(maybe(2), maybe(34)) == "234");
assert(match!((x, y)=>text(x, y), {})(Maybe!int(), maybe(34)) == null);
assert(match!((x, y)=>text(x, y), ()=>"none")(Maybe!int(), maybe(34)) ==
"none");
I'd be interested to see your Option code.
Nick
More information about the Digitalmars-d
mailing list