Conditionals
Conditionals use if, else if and else like many other languages.
Like other languages, parens around the condition are required.
if (condition) {
print("the thing is true")
}
The condition must be of type bool.
if (322) { // error
print("This does not work")
}
Else branch
Works as expected.
if (false) {
print("Does not work")
}
else {
print("Works")
}
Else if
Also works as expected.
if (first_condition) {
print("first")
}
else if (second_condition) {
print("second")
}
else {
print("otherwise")
}
Nullables
Conditionals can also be used to check if a nullable exists, and get the inner value:
val maybe_user: ?User = get_user(id)
if (maybe_user) |user| {
// in here `user` is transformed to `User`, no longer null
print(user.name)
}