elige negative patterns
Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.
A leading minus before a number literal is a case value in switch.
Syntax: switch <number> { case -1 ... case -2.5 ... case -∞ ... }
Category#
control-flow
Related#
Examples#
radix/corpus/elige/negative-patterns.fab (canonical · feature)#
A leading minus before a number literal is a case value in switch.
# 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