Conditionals
Conditional expressions must evaluate to Bool. Use Bool(x) to typecast, or implement __bool__ on classes.
If / Else
if !confirmed:
out = "not yet"
else:
out = "done"
Else If
if number > 5000:
return (out: "large")
else if number >= 2500:
return (out: "medium")
else:
return (out: "small")
Ternary Operator
Single-line conditional assignment:
out = (value if condition else fallback)
Example:
memory status = ("adult" if age >= 18 else "minor")
memory capped = (v if v < 100 else 100)
Switch
Match against multiple cases with optional default:
switch x * 2:
case 0:
s = "zero"
case 1, 2, 3:
s = "small"
case 24:
s = "twenty-four"
case x > 10 && x < 20:
s = "medium range"
default:
s = "other"
tip
Cases can be:
- Literal values:
case 0,case "yes" - Multiple values:
case 1, 2, 3 - Boolean expressions:
case x > 10 - Class instances (with
__eq__):case Person{name: "Alice"} - Function calls:
case len(s) == 3
Default is optional. If no case matches and no default, nothing happens.
Boolean Evaluation
Classes can participate in boolean contexts by implementing __bool__:
class Person:
field name String
field age U64
method __bool__() -> (result Bool):
result = self.age >= 18
endpoint CheckAge(person Person) -> (is_adult Bool):
is_adult = Bool(person) // Explicit
// or
if person: // Implicit
is_adult = true