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

Try expressions #412

Closed
bvssvni opened this issue Dec 3, 2016 · 0 comments
Closed

Try expressions #412

bvssvni opened this issue Dec 3, 2016 · 0 comments
Assignees

Comments

@bvssvni
Copy link
Member

bvssvni commented Dec 3, 2016

Sometimes you want to continue running after a function call fails:

// Get first item from current `arr`
foo() ~ arr = clone(arr[0])

fn main() {
    ~ arr := [] // `arr` is empty, so there is no first item.
    a := foo() // ERROR: Out of bounds `0`
    println(a)
}

A try expression lets you convert runtime failures into err and success into ok:

foo() ~ arr = clone(arr[0])

fn main() {
    ~ arr := []
    a := try foo()
    println(a) // prints `err(...)`
}

Try expressions are convenient together with the ? operator:

fn bar() -> res { ... }

fn main() {
    a := try bar()?
    if is_err(a) {
        // Calling `bar` failed.
        ...
    }
}

This maps err to early returns, and runtime failures to err:

ok(X), err(Y), fail(Z) => ok(X), err(Z), return(err(Y))
@bvssvni bvssvni self-assigned this Dec 3, 2016
bvssvni added a commit to bvssvni/dyon that referenced this issue Dec 3, 2016
Closes PistonDevelopers#412

- Fixed rule ids
- Added “syntax/try_expr.dyon”
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant