Grav v1.14.0

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()) }
}
Output
8080 true true 7
no port given

API

class Null

Values that may be null (see the top of this file).

Static members

  • static fn isNull<T>(v: T?) -> bool — Whether v is null.
  • static fn hasValue<T>(v: T?) -> bool — Whether v holds a value.
  • static fn orElse<T>(v: T?, fallback: T) -> T — v, or fallback when it is null.
  • static fn orElseGet<T>(v: T?, f: fn() -> T) -> T — v, or what f makes when it is null (f runs only then).
  • static fn require<T>(v: T?, message: string) -> T — v; ValueError with message when it is null.
  • static fn ifPresent<T>(v: T?, f: fn(T)) — Calls f with the value when there is one.

Edit this page on GitHub