Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ GEM
sorbet (0.5.11274)
sorbet-static (= 0.5.11274)
sorbet-runtime (0.5.11274)
sorbet-static (0.5.11274-universal-darwin)
sorbet-static (0.5.11274-x86_64-linux)
sorbet-static-and-runtime (0.5.11274)
sorbet (= 0.5.11274)
Expand Down Expand Up @@ -117,6 +118,7 @@ GEM
yard (>= 0.9)

PLATFORMS
arm64-darwin-23
x86_64-linux

DEPENDENCIES
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,19 @@ end

If you think it's possible to implement something closer to Rust's `?`, I'd love to hear about it! Feel free to open an issue or PR to start a discussion.

TODO: using `propagate!` and `try!`
```ruby
sig { params(info: Info).returns(R::Result[NilClass, StandardError]) }
def write_info(info)
R.propagate! do
file = file_create("my_best_friends.txt").try!
file_write_all(file, "name: #{info.name}\n").try!
file_write_all(file, "age: #{info.age}\n").try!
file_write_all(file, "rating: #{info.rating}\n").try!
end
end
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
33 changes: 33 additions & 0 deletions lib/r/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,9 @@ def unwrap_or_else(&blk); end
.returns(OkType)
end
def try?(&blk); end

sig { abstract.returns(T.self_type) }
def try!; end
end

# Creates a new instance of {Ok}.
Expand All @@ -597,6 +600,22 @@ def self.ok(value)
Ok.new(value)
end

sig do
type_parameters(:Ok, :Err)
.params(
blk: T.proc.returns(T.nilable(Result[T.type_parameter(:Ok), T.type_parameter(:Err)])),
)
.returns(Result[T.type_parameter(:Ok), T.type_parameter(:Err)])
end
def self.propagate!(&blk)
# TODO: using singleton class instance variables is obviously bad and not
# thread-safe but demonstrates how it might work.
@ball = T.let(Object.new, Object)
Copy link
Contributor Author

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.catch use symbols, it actually uses object_id to catch a thrown object. The block you pass to catch gets passed a unique object that is specifically intended to be thrown 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 new Object so that it will only catch this specific object's object_id and consumer code can't accidentally early-return if they use throw inside the block for some reason.

catch(@ball) { blk.call || R.ok(nil) } # rubocop:disable Performance/RedundantBlockCall
ensure
@ball = nil
end

# Contains the success value.
#
# @see R::Result
Expand Down Expand Up @@ -1102,6 +1121,11 @@ def try?(&blk)
@value
end

sig(:final) { override.returns(T.self_type) }
def try!
self
end

# Calls the provided block with the contained value.
#
# Returns `self` so this can be chained with {#on_err}.
Expand Down Expand Up @@ -1656,6 +1680,15 @@ def try?(&blk)
yield(self)
end

sig(:final) { override.returns(T.self_type) }
def try!
if (ball = R.instance_variable_get(:@ball))
throw(ball, self)
else
self
end
end

# Does nothing.
#
# Returns `self` so this can be chained with {#on_err}.
Expand Down
28 changes: 28 additions & 0 deletions test/r/result_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 Result from the block and there wouldn't be any type errors.

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)
Expand Down