Error Handling
Arden provides robust error handling using the Option<T> and Result<T, E> types.
Option
Represents a value that might be present or missing.
enum Option<T> {
Some(T),
None
}
Usage:
function findParams(id: Integer): Option<String> {
if (id == 0) {
return Option.None;
}
return Option.Some("Param");
}
Option Methods
is_some(): Boolean- Returnstrueif the option is aSomevalue.is_none(): Boolean- Returnstrueif the option is aNonevalue.unwrap(): T- Returns the inner value or panics ifNone.
Result<T, E>
Represents a success (Ok) or failure (Error).
enum Result<T, E> {
Ok(T),
Error(E)
}
Usage:
function divide(a: Integer, b: Integer): Result<Integer, String> {
if (b == 0) {
return Result.Error("Division by zero");
}
return Result.Ok(a / b);
}
Result Methods
is_ok(): Boolean- Returnstrueif the result isOk.is_error(): Boolean- Returnstrueif the result isError.unwrap(): T- Returns the success value or panics ifError.
The ? Operator
The ? operator simplifies error propagation. If a Result is Error, it returns early.
function computation(): Result<Integer, String> {
val: Integer = divide(10, 2)?; // Unwraps or returns Error
val2: Integer = divide(val, 0)?; // Returns Error("Division by zero")
return Result.Ok(val2);
}
arden check validates ? at the function boundary:
Option<T>?requires the enclosing function to returnOption<...>Result<T, E>?requires the enclosing function to returnResult<..., E-compatible>- nested lambda bodies are checked against their own lambda return context, not the outer function's
Option/Resultreturn type