null
null is the only value of type null. It marks an absent value. null is a keyword.
Details
- A type holds
nullonly when it says so:string | nullcan holdnull,stringcannot. There is noOptionwrapper and noundefined.
Basic examples
The literal, and a test for it:
null // null
null == null // trueA nullable variable, and ?? for a fallback:
let nickname: string | null = null
nickname ?? "anonymous" // "anonymous"Scope
Function results
A function that may find nothing returns T | null. Here find gives null when no user matches, and ?. passes it on:
type User = { id: int, name: string }
func findUser(users: Array[User], id: int): User | null {
users.find({ x.id == id })
}
let users = [User{ id: 1, name: "Ann" }, User{ id: 2, name: "Bob" }]
println(findUser(users, 2)?.name)
println(findUser(users, 7)?.name)Bob
nullNarrowing
After a test against null, the checker knows the variable is not null:
func greet(name: string | null): string {
if name == null { return "Hello, stranger" }
"Hello, ${name.trim()}"
}
println(greet(null))
println(greet(" Ann "))Hello, stranger
Hello, AnnA test narrows both branches of an if: after name != null, the then branch sees a string and the else branch sees null. name is null narrows the same way as name == null:
func greet(name: string | null): string {
if name != null { name.trim() } else { "stranger" }
}
greet(" Ann ") // "Ann"
greet(null) // "stranger"Default arguments and record fields
null is a common default for an optional parameter or field. A field never narrows, so label copies c.email into a let and tests the copy:
type Contact = { name: string, email: string | null = null }
func label(c: Contact, prefix: string | null = null): string {
let name = if prefix == null { c.name } else { "${prefix} ${c.name}" }
let email = c.email
if email == null { name } else { "${name} <${email}>" }
}
println(label(Contact{ name: "Ann" }))
println(label(Contact{ name: "Bob", email: "bob@example.com" }, prefix: "Dr."))Ann
Dr. Bob <bob@example.com>Collections
An array, a map, or a set can hold null when its element type includes it:
let scores = [90, null, 75]
scores // [90, null, 75]
scores.filter({ x != null }) // [90, 75]
Map{ (null): "none", 1: "one" } // Map{ null: "none", 1: "one" }
Set.of([null, 1]) // Set.of([null, 1])Rendering
null renders as null in a string and when printed:
let middle: string | null = null
"middle: ${middle}" // "middle: null"
String.from(null) // "null"Possible issues
A missing key and a stored null look the same
Indexing a map gives null both for a missing key and for a key that holds null. Ask the map with has:
let settings = Map{ "theme": null, "lang": "en" }
settings["theme"] // null
settings["font"] // null
settings.has("theme") // true
settings.has("font") // falseA plain type cannot hold null
let name: string = null // error: cannot use null where string is expectedA nullable value needs narrowing first
A field or function of T is not available on T | null:
let name: string | null = "Ann"
let size = name.length()
// error: string | null has no field or function length on every member: narrow the value firstTest it, or use ?. or ??:
let name: string | null = "Ann"
name?.length() // 3
(name ?? "").length() // 3A field is not narrowed
Narrowing works only for a local variable, a parameter, or a top-level let. A test on a field such as c.email does not narrow it:
type Contact = { name: string, email: string | null }
func emailLength(c: Contact): int {
if c.email == null { return 0 }
c.email.length()
// error: string | null has no field or function length on every member: narrow the value first
}Copy the field into a let and test the copy, or use ?.:
type Contact = { name: string, email: string | null }
func emailLength(c: Contact): int {
let email = c.email
if email == null { return 0 }
email.length()
}
func emailLength2(c: Contact): int {
c.email?.length() ?? 0
}
println(emailLength(Contact{ name: "Ann", email: "ann@example.com" }))
println(emailLength2(Contact{ name: "Bob", email: null }))15
0Testing a non-nullable value against null
== needs one side's type to be assignable to the other's, so a value that cannot be null cannot be compared with null:
let s = "a"
if s == null { println("empty") }
// error: string and null cannot be compared with ==: neither is assignable to the othervar x = null has type null
Without an annotation, the variable can hold nothing but null:
var cached = null
cached = "data" // error: cannot use string where null is expectedAnnotate the nullable type:
var cached: string | null = null
cached = "data"
cached // "data"?? on a value that cannot be null
?? and ?. need a nullable left side; on any other type they are dead code:
let name = "Ann"
let shown = name ?? "anonymous" // error: ?? needs a nullable left side, got stringA bare null key is a string
Map{ null: 1 } // Map{ "null": 1 }
Map{ (null): 1 } // Map{ null: 1 }null is not a variable name
let null = 0 // error: 'null' cannot be a variable name