Null
import "std/types" (included in import "std") · namespace std.types
Null's methods are the methods of any value that may be null (T?): a default, a check, or an error when it is missing. See Null safety for T? itself.
grav
import "std"
fn main() {
let port: int? = null
let id: long? = 7
print("${port.orElse(8080)} ${port.isNull()} ${id.hasValue()} ${id.require("no id")}")
try { print(port.require("no port given")) } catch (e: ValueError) { print(e.message()) }
}8080 true true 7
no port givenAPI
class Null
Values that may be null (see the top of this file).
Static members
static fn isNull<T>(v: T?) -> bool— Whethervis null.static fn hasValue<T>(v: T?) -> bool— Whethervholds a value.static fn orElse<T>(v: T?, fallback: T) -> T—v, orfallbackwhen it is null.static fn orElseGet<T>(v: T?, f: fn() -> T) -> T—v, or whatfmakes when it is null (f runs only then).static fn require<T>(v: T?, message: string) -> T—v; ValueError withmessagewhen it is null.static fn ifPresent<T>(v: T?, f: fn(T))— Callsfwith the value when there is one.