Class: R::Err
- Inherits:
-
Object
- Object
- R::Err
- Extended by:
- T::Generic, T::Helpers, T::Sig
- Includes:
- Result
- Defined in:
- lib/r/result.rb
Overview
Contains the error value.
Constant Summary collapse
- OkType =
The type of the success value.
In the context of an R::Err value, this is set to T.noreturn. This enables Sorbet to detect potential dead code paths.
type_member { { fixed: T.noreturn } }
- ErrType =
The type of the error value.
type_member
Class Method Summary collapse
-
.new(value) ⇒ T.all(T.attached_class, Err[T.type_parameter(:E)])
Creates a new instance of Err.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns
true
ifother
is both Err andself.err == other.err
. -
#and(res) ⇒ Result[T.type_parameter(:U), ErrType]
Returns the Err value of
self
. -
#and_then(&blk) ⇒ Result[T.type_parameter(:U), ErrType]
Returns the Err value of
self
. -
#err ⇒ ErrType
Returns the value.
-
#err? ⇒ true
Returns
true
. -
#err_and?(&blk) {|@value| ... } ⇒ Boolean
Returns
true
if the value matches a predicate. -
#expect!(msg) ⇒ T.noreturn
Raises an UnwrapFailedError.
-
#expect_err!(msg) ⇒ ErrType
Returns the contained Err value.
-
#initialize(value) ⇒ void
constructor
Creates a new instance of Err.
-
#inspect ⇒ String
Returns a string representation of
self
. -
#map(&blk) ⇒ Err[ErrType]
Returns
self
. - #map_err(&blk) ⇒ Err[T.type_parameter(:F)]
-
#map_or(default, &blk) ⇒ T.type_parameter(:U)
Returns the provided default.
- #map_or_else(default, &blk) ⇒ T.type_parameter(:U)
-
#ok ⇒ nil
Returns
nil
. -
#ok? ⇒ false
Returns
false
. -
#ok_and?(&blk) ⇒ false
Returns
false
. -
#on_err(&blk) {|@value| ... } ⇒ T.self_type
Calls the provided block with the contained value.
-
#on_ok(&blk) ⇒ T.self_type
Does nothing.
-
#or(res) ⇒ Result[T.type_parameter(:U), T.type_parameter(:F)]
Returns
res
. -
#or_else(&blk) {|@value| ... } ⇒ Result[T.type_parameter(:U), T.type_parameter(:F)]
Calls the block.
-
#try?(&blk) {|_self| ... } ⇒ T.noreturn
Calls the block with the Err value.
-
#unwrap! ⇒ T.noreturn
Raises an UnwrapFailedError.
-
#unwrap_err! ⇒ ErrType
Returns the contained Err value.
-
#unwrap_or(default) ⇒ T.type_parameter(:DefaultType)
Returns the provided default.
-
#unwrap_or_else(&blk) {|@value| ... } ⇒ T.type_parameter(:DefaultType)
Computes a value from a closure.
Constructor Details
#initialize(value) ⇒ void
Creates a new instance of R::Err.
1236 1237 1238 |
# File 'lib/r/result.rb', line 1236 def initialize(value) @value = value end |
Class Method Details
Instance Method Details
#==(other) ⇒ Boolean
Returns true
if other
is both R::Err and self.err == other.err
.
1190 1191 1192 1193 1194 1195 1196 1197 |
# File 'lib/r/result.rb', line 1190 def ==(other) case other when Err other.err == @value else false end end |
#and(res) ⇒ Result[T.type_parameter(:U), ErrType]
1499 1500 1501 |
# File 'lib/r/result.rb', line 1499 def and(res) self end |
#and_then(&blk) ⇒ Result[T.type_parameter(:U), ErrType]
1528 1529 1530 |
# File 'lib/r/result.rb', line 1528 def and_then(&blk) self end |
#err ⇒ ErrType
Returns the value.
1317 1318 1319 |
# File 'lib/r/result.rb', line 1317 def err @value end |
#err? ⇒ true
Returns true
.
1275 1276 1277 |
# File 'lib/r/result.rb', line 1275 def err? true end |
#err_and?(&blk) {|@value| ... } ⇒ Boolean
Returns true
if the value matches a predicate.
1291 1292 1293 |
# File 'lib/r/result.rb', line 1291 def err_and?(&blk) yield(@value) end |
#expect!(msg) ⇒ T.noreturn
Raises an UnwrapFailedError.
1432 1433 1434 |
# File 'lib/r/result.rb', line 1432 def expect!(msg) raise UnwrapFailedError.new(msg, @value) end |
#expect_err!(msg) ⇒ ErrType
Returns the contained R::Err value.
1460 1461 1462 |
# File 'lib/r/result.rb', line 1460 def expect_err!(msg) @value end |
#inspect ⇒ String
Returns a string representation of self
.
1208 1209 1210 1211 1212 1213 1214 1215 1216 |
# File 'lib/r/result.rb', line 1208 def inspect value_repr = case @value when Object @value.inspect else "<uninspectable value>" end "R.err(#{value_repr})" end |
#map(&blk) ⇒ Err[ErrType]
Returns self
.
This function can be used to compose the results of two functions.
1333 1334 1335 |
# File 'lib/r/result.rb', line 1333 def map(&blk) self end |
#map_err(&blk) ⇒ Err[T.type_parameter(:F)]
1411 1412 1413 |
# File 'lib/r/result.rb', line 1411 def map_err(&blk) Err.new(yield(@value)) end |
#map_or(default, &blk) ⇒ T.type_parameter(:U)
Returns the provided default.
Arguments passed to #map_or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use #map_or_else, which is lazily evaluated.
1357 1358 1359 |
# File 'lib/r/result.rb', line 1357 def map_or(default, &blk) default end |
#map_or_else(default, &blk) ⇒ T.type_parameter(:U)
1382 1383 1384 |
# File 'lib/r/result.rb', line 1382 def map_or_else(default, &blk) default.call(@value) end |
#ok ⇒ nil
Returns nil
.
1304 1305 1306 |
# File 'lib/r/result.rb', line 1304 def ok nil end |
#ok? ⇒ false
Returns false
.
1249 1250 1251 |
# File 'lib/r/result.rb', line 1249 def ok? false end |
#ok_and?(&blk) ⇒ false
Returns false
.
1262 1263 1264 |
# File 'lib/r/result.rb', line 1262 def ok_and?(&blk) false end |
#on_err(&blk) {|@value| ... } ⇒ T.self_type
Calls the provided block with the contained value.
Returns self
so this can be chained with #on_ok.
1689 1690 1691 1692 |
# File 'lib/r/result.rb', line 1689 def on_err(&blk) yield(@value) self end |
#on_ok(&blk) ⇒ T.self_type
Does nothing.
Returns self
so this can be chained with #on_err.
1672 1673 1674 |
# File 'lib/r/result.rb', line 1672 def on_ok(&blk) self end |
#or(res) ⇒ Result[T.type_parameter(:U), T.type_parameter(:F)]
Returns res
.
Arguments passed to or
are eagerly evaluated; if you are passing the result of a function call, it is
recommended to use #or_else, which is lazily evaluated.
1554 1555 1556 |
# File 'lib/r/result.rb', line 1554 def or(res) res end |
#or_else(&blk) {|@value| ... } ⇒ Result[T.type_parameter(:U), T.type_parameter(:F)]
Calls the block.
This function can be used for control flow based on Result values.
1588 1589 1590 |
# File 'lib/r/result.rb', line 1588 def or_else(&blk) yield(@value) end |
#try?(&blk) {|_self| ... } ⇒ T.noreturn
Calls the block with the R::Err value.
This method is similar to #unwrap_or_else, but in case of an R::Err value, the block must short-circuit by
either calling return
(which will return from the enclosing method) or raising an exception.
This is useful to make error handling less tedious when dealing with many methods returning results.
1655 1656 1657 |
# File 'lib/r/result.rb', line 1655 def try?(&blk) yield(self) end |
#unwrap! ⇒ T.noreturn
Raises an UnwrapFailedError.
1447 1448 1449 |
# File 'lib/r/result.rb', line 1447 def unwrap! raise UnwrapFailedError.new("called `Result#unwrap!` on an `Err` value", @value) end |
#unwrap_err! ⇒ ErrType
Returns the contained R::Err value.
1473 1474 1475 |
# File 'lib/r/result.rb', line 1473 def unwrap_err! @value end |
#unwrap_or(default) ⇒ T.type_parameter(:DefaultType)
Returns the provided default.
Arguments passed to unwrap_or
are eagerly evaluated; if you are passing the result of a function call, it is
recommended to use #unwrap_or_else, which is lazily evaluated.
1611 1612 1613 |
# File 'lib/r/result.rb', line 1611 def unwrap_or(default) default end |
#unwrap_or_else(&blk) {|@value| ... } ⇒ T.type_parameter(:DefaultType)
Computes a value from a closure.
1629 1630 1631 |
# File 'lib/r/result.rb', line 1629 def unwrap_or_else(&blk) yield(@value) end |