Kết xuấtvi

hoặc_nếu_rỗng

Translation status: Tiếng Việt reader-locale proof. Term names and code fences follow the vi pack; supporting prose may still be English.

Provides a default when the left side is null.

Aliases: nullish coalescing

Syntax: <expression> hoặc_nếu_rỗng <expression>

Category#

logic

Examples#

radix/corpus/vel/vel.fab (canonical · operator-group)#

Provides a default when the left side is null.

test "vel supplies a fallback for nihil" {
    const string ∪ none tên ← null
    let ostensum ← tên 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" {
    const string ∪ none a ← null
    const string ∪ none b ← null
    const string ∪ none c ← "inventum"
    let primum ← ((a coalesce b) coalesce c) coalesce "nihil"
    assert primum ≡ "inventum"
}
# =============================================================================
# vel — Provides a default when the left side is null.
# =============================================================================
#
# What this teaches:
#   • Nullish coalescing — `vel` supplies a fallback when the left operand is `rỗng_ty`
#   • Chaining — multiple `vel` expressions fall through until the first non-`rỗng_ty` value
#   • Short-circuit — like `et`/`hoặc`, the right operand runs only when the left is `rỗng_ty`
#
# Common mistakes:
#   • confusing vel (nullish coalescing, checks rỗng_ty only) with hoặc (logical OR), or using vel for chuyển_đổi recovery instead of ⇥
#
# See also: rỗng_ty
# =============================================================================




# vel — nullish coalescing (not logical OR)
#
# <expr> vel <expr>   -- right side only when left is rỗng_ty
#
# 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 { return 7 }
    return null
}

fn supplementum(int giá_trị) → int {
    print "supplementum"
    return giá_trị
}

main {
    # Basic rỗng_ty coalescing
    const string ∪ none tên ← null
    let ostensum ← tên coalesce "ignotus"
    # "ignotus"
    print ostensum

    # With present value
    const string ∪ none adest ← "Marcus"
    let ostensum2 ← adest coalesce "ignotus"
    # "Marcus"
    print ostensum2

    # Difference from logical hoặc: vel only checks rỗng_ty
    let nihilum ← 0
    let vacuus ← ""
    let falsus2 ← false

    # vel preserves non-nihil values
    # 0
    print nihilum coalesce 999
    # ""
    print vacuus coalesce "defectum"
    # sai
    print falsus2 coalesce true
    let alter ← tên coalesce "defectum"
    print alter

    # Chaining
    const string ∪ none a ← null
    const string ∪ none b ← null
    const 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 rỗng_ty
    # 7 (no "supplementum" line)
    print fortasse(true) coalesce supplementum(9)
    # "supplementum", then 9
    print fortasse(false) coalesce supplementum(9)
}

Expected output:

ignotus
Marcus
0

falsum
defectum
inventum
7
supplementum
9