union Animal { Dog(String), Cat(String, Int), } val my_pet = try Pets::get_first() match my_pet case Dog(name) { print("{name} has 1 live ") } case Cat(name, lives) { print("{name} has {lives}") }
Array[Int] numbers = [0, 1, 2, 3, 4, 5]
numbers.push("7") // Compile error: expected an Int, found a String
// Runtime type checking
val object = try JSON::decode(data)
val name = try object.get[String]("name")
// Any is available, but it's not usable without an explicit cast
fun mock() -> Any { ... }
// Compile error: Cannot use `Any` without an explicit cast
val result = mock()
// Ok
val result = mock() as String
thp
union DirEntry {
File(String),
Dir(String),
}
val root = DirEntry::Dir("/")
val test_file = DirEntry::File("test.txt")
thp
match test_file
case DirEntry::File(filename)
if !filename.starts_with(".") {
print(filename)
}
case _ {
print("A valid file was not found")
}
thp
String? username = Post::get("username")
if username? {
// username is a `String` here
print("Hello, {username}")
}
thp
val user_id = POST::get("id")
val user = try User::find(user_id)
catch DBException e {
log_error(e.message)
return page(.{}, 404)
}
thp