fun(t.get).nullable if !t.isNull, else Nullable.init.
See also: The Maybe monad
alias toFloat = i => cast(float) i; Nullable!int sample; // apply(null) results in a null `Nullable` of the function's return type. Nullable!float f = sample.apply!toFloat; assert(sample.isNull && f.isNull); sample = 3; // apply(non-null) calls the function and wraps the result in a `Nullable`. f = sample.apply!toFloat; assert(!sample.isNull && !f.isNull); assert(f.get == 3.0f);
1 alias greaterThree = i => (i > 3) ? i.nullable : Nullable!(typeof(i)).init; 2 3 Nullable!int sample; 4 5 // when the function already returns a `Nullable`, that `Nullable` is not wrapped. 6 auto result = sample.apply!greaterThree; 7 assert(sample.isNull && result.isNull); 8 9 // The function may decide to return a null `Nullable`. 10 sample = 3; 11 result = sample.apply!greaterThree; 12 assert(!sample.isNull && result.isNull); 13 14 // Or it may return a value already wrapped in a `Nullable`. 15 sample = 4; 16 result = sample.apply!greaterThree; 17 assert(!sample.isNull && !result.isNull); 18 assert(result.get == 4);
Unpacks the content of a Nullable, performs an operation and packs it again. Does nothing if isNull.
When called on a Nullable, apply will unpack the value contained in the Nullable, pass it to the function you provide and wrap the result in another Nullable (if necessary). If the Nullable is null, apply will return null itself.