module arsd.generic;

class NullPointerException : Exception {
	this() {
		super("A null pointer was detected");
	}
}

/// Segfaults are kinda annoying in the web environment so we use this instead.
T notNull(T)(T t) {
	if(t is null)
		throw new NullPointerException();
	return t;
}

class CastingException : Exception {
	this(string upperClass, string lowerClass) {
		super("Couldn't cast instance of " ~ lowerClass ~ " to " ~ upperClass);
	}
}

/// Does an upcast, but throws instead of returning null if it fails
T upcast(T, R)(R r) if(is(T : R)) {
	auto t = cast(T) r;
	if(t is null)
		throw new CastingException(T.stringof, R.stringof);
	return t;
}
Suggestion Box / Bug Report