Generic optional value container. Represents a value that may or may not exist.
import core.option
let some = Some(42) // Option with value
let none = None<Int>() // Empty option
let opt = Option<Int>.New() // Empty option (alternative)
| Method | Return | Description |
|---|---|---|
isSome() |
Bool |
True if value exists |
isNone() |
Bool |
True if empty |
unwrap() |
T |
Get value (panics if empty) |
expect(message: String) |
T |
Get value (panics with message if empty) |
unwrapOr(default: T) |
T |
Get value or return default |
Store(value: T) |
Void |
Set the value |
Replace(value: T) |
Option<T> |
Replace value, return old |
Clear() |
Void |
Remove value |
fun findUser(id: Int) r: Option<String> {
if id == 1 {
return Some("Alice")
}
return None<String>()
}
fun main() {
let user = findUser(1)
if user.isSome() {
println("Found: {user.unwrap()}")
}
let name = findUser(99).unwrapOr("Unknown")
}
Container for success/error values.
import core.result
let ok = Ok<Int, String>(42)
let err = Err<Int, String>("not found")
| Method | Return | Description |
|---|---|---|
isOkay() |
Bool |
True if success |
isErr() |
Bool |
True if error |
unwrap() |
T |
Get success value (panics if error) |
expect(message: String) |
T |
Get value (panics with message if error) |
unwrapOr(default: T) |
T |
Get value or return default |
unwrapErr() |
E |
Get error value (panics if success) |
swap() |
Result<E, T> |
Swap Ok and Err types |
? OperatorPropagates errors:
fun process() r: Result<Int, String> {
let x = parse("42")? // returns Err early if parse fails
return Ok(x * 2)
}
fun divide(a: Int, b: Int) r: Result<Int, String> {
if b == 0 {
return Err("division by zero")
}
return Ok(a / b)
}
fun main() {
let result = divide(10, 3)
if result.isOkay() {
println("Result: {result.unwrap()}")
} else {
println("Error: {result.unwrapErr()}")
}
}
The unit type, used as a placeholder when no value is needed:
import core.unit
let u = unit()
Useful as the value type in Result<Unit, E> or map types like HashMap<String, Unit> (set semantics).
Comparison result type:
import core.ord
enum Ordering {
Less
Equal
Greater
}
| Function | Signature | Description |
|---|---|---|
compareInt |
(a: Int, b: Int) r: Ordering |
Compare two integers |
compareString |
(a: String, b: String) r: Ordering |
Lexicographic string comparison |
isLess |
(ord: Ordering) r: Bool |
Check if Less |
isEqual |
(ord: Ordering) r: Bool |
Check if Equal |
isGreater |
(ord: Ordering) r: Bool |
Check if Greater |
import core.convert
| Function | Signature |
|---|---|
Int_from_Float |
(value: Float) r: Int |
Int_from_String |
(value: String) r: Int |
Int_from_Bool |
(value: Int) r: Int |
| Function | Signature |
|---|---|
Float_from_Int |
(value: Int) r: Float |
Float_from_String |
(value: String) r: Float |
| Function | Signature |
|---|---|
String_from_Int |
(value: Int) r: String |
String_from_Float |
(value: Float) r: String |
String_from_Bool |
(value: Int) r: String |
aslet x: Int = 42
let f = x as Float // 42.0
let s = x as String // "42"
@trait class ToString {
fun toString() r: String
}
Free function converters:
Int_toString(value: Int) r: StringFloat_toString(value: Float) r: StringBool_toString(value: Int) r: StringChar_toString(value: Int) r: String