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

[Feature Request] [stdlib] [proposal] Add ErrorReg, Result, and ResultReg types #2748

Open
1 task done
martinvuyk opened this issue May 18, 2024 · 0 comments
Open
1 task done
Labels
enhancement New feature or request mojo-repo Tag all issues with this label needs-discussion Need discussion in order to move forward

Comments

@martinvuyk
Copy link

martinvuyk commented May 18, 2024

Review Mojo's priorities

What is your request?

Result is basically a copy paste of Optional just with an error attribute.

"""Defines Result, a type modeling a value which may or may not be present.
With an Error in the case of failure.

Result values can be thought of as a type-safe nullable pattern.
Your value can take on a value or None, and you need to check
and explicitly extract the value to get it out.

from collections import Result
var a = Result(1)
var b = Result[Int]()
if a:
    print(a.value()[])  # prints 1
if b:  # bool(b) is False, so no print
    print(b.value()[])
var c = a.or_else(2)
var d = b.or_else(2)
print(c)  # prints 1
print(d)  # prints 2

And if more information about the returned Error is wanted it is available.

from collections import Result
var a = Result(1)
var b = Result[Int](err=Error("something went wrong"))
var c = Result[Int](err=Error("error 1"))
var d = Result[Int](err=Error("error 2"))
if a:
    print(a.err)  # prints ""
if not b:
    print(b.err) # prints "something went wrong"

if c.err:
    print("c had an error")

# TODO: pattern matching
if d.err == "error 1":
    print("d had error 1")
elif d.err == "error 2":
    print("d had error 2")

A Result with an Error cal also be retuned early:

fn func_that_can_err[A: CollectionElement]() -> Result[A]:
    ...

fn return_early_if_err[T: CollectionElement, A: CollectionElement]() -> Result[T]:
    var result: Result[A] = func_that_can_err[A]()
    if not result:
        # the internal err gets transferred to a Result[T]
        return result
        # its also possible to do:
        # return None, Error("func_that_can_err failed")
    var val = result.value()
    var final_result: T
    ...
    return final_result

What is your motivation for this change?

Better Error handling than raising and giving people tools to write nicer code

Any other details?

I skimmed over the Error struct's code and it seems it can be done replacing its fields with just a StringLiteral (?)

@martinvuyk martinvuyk added enhancement New feature or request mojo-repo Tag all issues with this label labels May 18, 2024
@JoeLoser JoeLoser added the needs-discussion Need discussion in order to move forward label May 21, 2024 — with Linear
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request mojo-repo Tag all issues with this label needs-discussion Need discussion in order to move forward
Projects
None yet
Development

No branches or pull requests

2 participants