Error Handling — Mapanare
Result and Option types with the ? operator — no exceptions, no null.
No Exceptions, No Null
Mapanare has no exceptions, no try/catch, and no null pointers. Errors are values — represented by Result<T, E> for operations that can fail, and Option<T> for values that may be absent.
Result Type
fn divide(a: Float, b: Float) -> Result<Float, String> {
if b == 0.0 { return Err("division by zero") }
return Ok(a / b)
}
The ? Operator
Unwraps a Result or Option — if it's an error/none, it returns early from the current function:
fn compute(input: String) -> Result<Float, String> {
let n = parse_int(input)? // returns Err early if parsing fails
let result = divide(n, 3.0)? // returns Err early if division fails
return Ok(result)
}
Option Type
fn find_user(id: Int) -> Option<String> {
if id == 1 { return Some("Alice") }
return none
}