การเรนเดอร์th-TH

หรือว่าง

Translation status: ภาษาไทย reader-locale proof. Term names and code fences follow the th-TH pack; supporting prose may still be English.

Provides a default when the left side is null.

Aliases: nullish coalescing

Syntax: <expression> หรือว่าง <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 ชื่อ ← null
    let ostensum ← ชื่อ 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 `นัล`
#   • Chaining — multiple `vel` expressions fall through until the first non-`นัล` value
#   • Short-circuit — like `et`/`หรือ`, the right operand runs only when the left is `นัล`
#
# Common mistakes:
#   • confusing vel (nullish coalescing, checks นัล only) with หรือ (logical OR), or using vel for การแปลง recovery instead of ⇥
#
# See also: นัล
# =============================================================================




# vel — nullish coalescing (not logical OR)
#
# <expr> vel <expr>   -- right side only when left is นัล
#
# 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 ค่า) → int {
    print "supplementum"
    return ค่า
}

main {
    # Basic นัล coalescing
    const string ∪ none ชื่อ ← null
    let ostensum ← ชื่อ coalesce "ignotus"
    # "ignotus"
    print ostensum

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

    # Difference from logical หรือ: vel only checks นัล
    let nihilum ← 0
    let vacuus ← ""
    let falsus2 ← false

    # vel preserves non-nihil values
    # 0
    print nihilum coalesce 999
    # ""
    print vacuus coalesce "defectum"
    # เท็จ
    print falsus2 coalesce true
    let alter ← ชื่อ 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 นัล
    # 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