coalesce
Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.
Provides a default when the left side holds null.
Aliases: nullish coalescing
Syntax: <expression> coalesce <expression>
Category#
logic
Related#
Examples#
radix/corpus/reference/operator-group/vel/vel.fab (canonical · operator-group)#
Provides a default when the left side holds null.
test "vel supplies a fallback for nihil" {
let string ∪ none name ← null
let _ ostensum ← name coalesce "ignotus"
assert ostensum ≡ "ignotus"
}
test "vel preserves non-nihil values" {
let _ nihilum ← 0
let _ vacuus ← ""
let _ falsus2 ← false
assert nihilum coalesce 999 ≡ 0
assert vacuus coalesce "defectum" ≡ ""
assert falsus2 coalesce true ≡ false
}
test "vel chains to the first non-nihil value" {
let string ∪ none a ← null
let string ∪ none b ← null
let string ∪ none c ← "inventum"
let _ primum ← ((a coalesce b) coalesce c) coalesce "nihil"
assert primum ≡ "inventum"
}
# =============================================================================
# vel — Provides a default when the left side holds `null`.
# =============================================================================
#
# What this teaches:
# • Nullish coalescing — `vel` supplies a fallback when the left operand holds `null` (the `none` type's value)
# • Chaining — multiple `vel` expressions fall through until the first non-`null` value
# • Short-circuit — like `et`/`or`, the right operand runs only when the left holds `null`
#
# Common mistakes:
# • confusing vel (nullish coalescing, checks for `null` only) with or (logical OR), or using vel for conversion recovery instead of ⇥
#
# See also: none
# =============================================================================
# vel — nullish coalescing (not logical OR)
#
# <expr> vel <expr> -- right side only when left is none
#
# GRAMMAR:
# coalesceExpr :← expr 'vel' expr
#
# EXPECTED OUTPUT:
# ignotus, Marcus, 0, false, defectum, inventum, 7, supplementum, 9 (vel.expected).
fn fortasse(bool adest) → int ∪ none {
if adest then return 7
return null
}
fn supplementum(int value) → int {
print "supplementum"
return value
}
main {
# Basic none coalescing
let string ∪ none name ← null
let _ ostensum ← name coalesce "ignotus"
# "ignotus"
print ostensum
# With present value
let string ∪ none adest ← "Marcus"
let _ ostensum2 ← adest coalesce "ignotus"
# "Marcus"
print ostensum2
# Difference from logical or: vel only checks none
let _ nihilum ← 0
let _ vacuus ← ""
let _ falsus2 ← false
# vel preserves non-nihil values
# 0
print nihilum coalesce 999
# ""
print vacuus coalesce "defectum"
# false
print falsus2 coalesce true
let _ alter ← name coalesce "defectum"
print alter
# Chaining
let string ∪ none a ← null
let string ∪ none b ← null
let string ∪ none c ← "inventum"
let _ primum ← ((a coalesce b) coalesce c) coalesce "nihil"
# "inventum"
print primum
# Short-circuit: the fallback runs only when the left side is none
# 7 (no "supplementum" line)
print fortasse(true) coalesce supplementum(9)
# "supplementum", then 9
print fortasse(false) coalesce supplementum(9)
}Expected output:
ignotus
Marcus
0
false
defectum
inventum
7
supplementum
9