mers/site/welcome.mers
2023-06-26 17:53:27 -04:00

52 lines
1.3 KiB
Plaintext
Executable File

fn get_number_input(question string) {
println(question)
input := read_line()
// try to parse to an int, then a float.
in := match {
input.parse_int() n n
input.parse_float() n n
}
// 'in' has type int/float/[] because of the match statement
switch! in {
int/float in in
// replace [] with an appropriate error before returning
[] [] Err: "input was not a number."
}
// return type is int/float/Err(string)
}
answer := get_number_input("What is your favorite number?")
answer.debug() // type: int/float/Err(string)
// switch can be used to branch based on a variables type.
// switch! indicates that every possible type must be handled.
switch! answer {
int num {
println("Entered an integer")
num.debug() // type: int
}
float num {
println("Entered a decimal number")
num.debug() // type: float
}
Err(string) [] println("Input was not a number!")
}
// wait one second
sleep(1)
// function that returns an anonymous function (function object).
// anonymous functions can be used as iterators in for-loops.
fn square_numbers() {
i := 0
() {
&i = i + 1
i * i
}
}
for num square_numbers() {
println(num.to_string())
// once num is greater than 60, the loop stops.
num.gt(50)
}