Expressions Everywhere
Blorp uses expressions for return values and ordinary decisions.
Basic Expressions
An expression produces a value. Literals such as 42 and "hello" are expressions, as are calculations such as (2 + 3) * 4.
number: Int = 42
text: String = "hello"
total: Int = (2 + 3) * 4
print(number.to_string()) -- prints: 42
print(text) -- prints: hello
print(total.to_string()) -- prints: 20
`if` As A Value
An if/else expression chooses a value. Each branch ends with an expression of the same type, so the result can be bound to a name.
score: Int = 82
label: String = if score >= 70:
"passing"
else:
"retry"
print(label) -- prints: passing
`match` As A Value
A match expression chooses a value from the matching case. Cover every possible input and give each case a result of the same type.
ready: Bool = True
message: String = match ready:
True:
"ready"
False:
"waiting"
print(message) -- prints: ready
Last Expression Returns
The final expression in a function body supplies its return value when the return type is not Void. That expression can be a calculation, an if, or a match; there is no return statement.
func double(n: Int) -> Int:
n * 2
func main(args: List[String]) -> Void:
print(double(21).to_string()) -- prints: 42
Statement-Only Conditionals
An if used only for effects can omit else.
score: Int = 65
if score < 70:
print("try again") -- prints: try again
Loops Return Void
for and while are for repeated work; values inside loop bodies are discarded after evaluation.
scores: List[Int] = [1, 2]
for score in scores:
print(score.to_string()) -- prints: 1, then 2 on separate lines
Example
pure func classify(temp: Int) -> String:
if temp < 50:
"cold"
else if temp < 80:
"warm"
else:
"hot"
func main(args: List[String]) -> Void:
print(classify(72)) -- prints: warm
Try it
blorp run temperature.brp