渲染zh-Hant

discerne type patterns

Translation status: 繁體中文 reader-locale proof. Term names and code fences follow the zh-Hant pack; supporting prose may still be English.

Discerne cases can test nominal type members and bind the narrowed value.

Syntax: 比對 <union> { 分支 <type> 定值 <name> { ... } }

Category#

control-flow

Examples#

radix/corpus/discerne/type-patterns.fab (canonical · feature)#

Discerne cases can test nominal type members and bind the narrowed value.

# Type-pattern cases use the same member predicate as `是` and bind the
# narrowed value for the arm body.
class Formal {
    var string greeting
}

class Casual {
    var string greeting
}

fn classify(Formal ∪ Casual u) → string {
    match u {
        case Formal const f {
            return f.greeting
        }
        case Casual const c {
            return c.greeting
        }
    }
}

main {
    const Formal f ← Formal { greeting = "formal" }
    const Casual c ← Casual { greeting = "casual" }
    print classify(f)
    print classify(c)
}

Expected output:

formal
casual