Adding `Result<V, E>` type, with `try` expressions
Closed this issue · 0 comments
kengorab commented
Add a builtin Result
monad to the stdlib's prelude:
enum Result<V, E> {
Ok(value: V)
Err(error: E)
}
This is the primary way of passing errors around in Abra, since errors are values (there are no exceptions). However, this idea is pretty unwieldy to work with without some kind of language built-in to help bubble up failures. This is the try
expression:
import readFile, ReadFileError from io
func readFile(): Result<String, ReadFileError> {
val contents = try readFile("./file.txt")
return process(contents)
}
Basically, this is transformed to the following code:
import readFile, ReadFileError from io
func readFile(): Result<String, ReadFileError> {
val contents = match readFile("./file.txt") {
Result.Ok(v) => v
Result.Err e => return e
}
return process(contents)
}
This is related to #290, but I think this is a more fully-baked idea. There can definitely be other things that implement this try
"interface" (though that's probably going to be re-written in a separate issue).