Kết xuấtvi

elige negative patterns

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

A leading minus before a number literal is a case value in chọn.

Syntax: chọn <number> { trường_hợp -1 ... trường_hợp -2.5 ... trường_hợp -∞ ... }

Category#

control-flow

Examples#

radix/corpus/elige/negative-patterns.fab (canonical · feature)#

A leading minus before a number literal is a case value in chọn.

# A number literal never carries its own sign, so the case value owns the minus.
# It covers integers, decimals, and negative infinity.
fn classify(int n) → string {
    switch n {
        case -1 {
            return "minus one"
        }
        case 0 {
            return "zero"
        }
        default {
            return "other"
        }
    }
}

fn describe(float x) → string {
    switch x {
        case -∞ {
            return "negative infinity"
        }
        case -2.5 {
            return "minus two and a half"
        }
        default {
            return "finite"
        }
    }
}

main {
    print classify(-1)
    print classify(0)
    print classify(7)
    print describe(-∞)
    print describe(-2.5)
    print describe(1.0)

    # The same negative literals work on a local value.
    const int floor ← -3
    switch floor {
        case -3 {
            print "basement three"
        }
        case -1 {
            print "basement one"
        }
        default {
            print "above ground"
        }
    }
}

Expected output:

minus one
zero
other
negative infinity
minus two and a half
finite
basement three