getOr

Gets the value inside of a nullable type or fallback to a given value

T
getOr
(
T
)
(
Nullable!T n
,
T t
)

Parameters

n Nullable!T

nullable type

t T

fallback value

Return Value

Type: T

value inside nullable if defined, fallback otherwise

Examples

auto a = nullable(3);
auto b = Nullable!(int).init;

assertEquals(3, a.getOr(7));
assertEquals(8, b.getOr(8));
auto foobar = () => "thestring";
// type independent
immutable string expr1 = Optional!(string).autoReturn!("foobar()");
immutable string expr2 = Optional!(typeof(null)).autoReturn!("foobar()");

import std.string : splitLines, strip, startsWith;
import std.algorithm.iteration : map, filter, joiner;

auto minify = (string t) => t.splitLines
	.map!(strip)
	.filter!(l => !l.empty)
	.filter!(l => !l.startsWith("//"))
	.joiner;

auto minifiedExpr = minify(q{
	auto ref expr() {
			return foobar();
		}
		alias R = typeof(expr());
		static if (!is(R == void))
			return empty ? none!R : some!R(expr());
		else {
			if (!empty) {
				expr();
			}
		}
});

// just to make sure you dont make mistakes on minify
assertFalse(minifiedExpr.array.empty);
assertFalse(minify(expr1).array.empty);
assertFalse(minify(expr2).array.empty);

assertEquals(minifiedExpr.array, minify(expr1).array);
assertEquals(minifiedExpr.array, minify(expr2).array);

Meta