Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Result<V, E> type, with try expressions #337

Open
kengorab opened this issue Sep 3, 2021 · 0 comments
Open

Adding Result<V, E> type, with try expressions #337

kengorab opened this issue Sep 3, 2021 · 0 comments

Comments

@kengorab
Copy link
Owner

kengorab commented Sep 3, 2021

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).

This was referenced Sep 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant