-
Notifications
You must be signed in to change notification settings - Fork 1
Use throw to implement Rust-like propagation #2
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,34 @@ | |
|
|
||
| module R | ||
| class ResultTest < Minitest::Test | ||
| describe "R.propagate!" do | ||
| it "propagates the first try! error" do | ||
| ok1 = T.let(R.ok(1), R::Result[Integer, String]) | ||
| err1 = T.let(R.err("err1"), R::Result[Integer, String]) | ||
| err2 = T.let(R.err("err2"), R::Result[Integer, String]) | ||
|
|
||
| result = R.propagate! do | ||
| ok1.try! | ||
| err1.try! | ||
| err2.try! | ||
| end | ||
|
|
||
| assert_equal(R.err("err1"), result) | ||
| end | ||
|
|
||
| it "propagates the last try! ok" do | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI, this test is deceiving. I think it's possible to do better, but this code actually just returns the result of the block, whatever that is. Since there's no runtime type checking of generics, I think you could just return an arbitrary |
||
| ok1 = T.let(R.ok(1), R::Result[Integer, String]) | ||
| ok2 = T.let(R.ok(2), R::Result[Integer, String]) | ||
|
|
||
| result = R.propagate! do | ||
| ok1.try! | ||
| ok2.try! | ||
| end | ||
|
|
||
| assert_equal(R.ok(2), result) | ||
| end | ||
| end | ||
|
|
||
| describe "R.ok" do | ||
| it "returns a new instance of R::Ok" do | ||
| x = R.ok(0) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though most examples online of
Kernel.catchuse symbols, it actually usesobject_idto catch a thrown object. The block you pass tocatchgets passed a unique object that is specifically intended to bethrown for this purpose, but it seemed imprudent to expose that object to the block that the consumer's of this library see, so instead this just creates a newObjectso that it will only catch this specific object'sobject_idand consumer code can't accidentally early-return if they usethrowinside the block for some reason.