class Concurrent::Maybe

A ‘Maybe` encapsulates an optional value. A `Maybe` either contains a value of (represented as `Just`), or it is empty (represented as `Nothing`). Using `Maybe` is a good way to deal with errors or exceptional cases without resorting to drastic measures such as exceptions.

‘Maybe` is a replacement for the use of `nil` with better type checking.

For compatibility with {Concurrent::Concern::Obligation} the predicate and accessor methods are aliased as ‘fulfilled?`, `rejected?`, `value`, and `reason`.

## Motivation

A common pattern in languages with pattern matching, such as Erlang and Haskell, is to return either a value or an error from a function Consider this Erlang code:

“‘erlang case file:consult(“data.dat”) of

{ok, Terms} -> do_something_useful(Terms);
{error, Reason} -> lager:error(Reason)

end. “‘

In this example the standard library function ‘file:consult` returns a [tuple](erlang.org/doc/reference_manual/data_types.html#id69044) with two elements: an [atom](erlang.org/doc/reference_manual/data_types.html#id64134) (similar to a ruby symbol) and a variable containing ancillary data. On success it returns the atom `ok` and the data from the file. On failure it returns `error` and a string with an explanation of the problem. With this pattern there is no ambiguity regarding success or failure. If the file is empty the return value cannot be misinterpreted as an error. And when an error occurs the return value provides useful information.

In Ruby we tend to return ‘nil` when an error occurs or else we raise an exception. Both of these idioms are problematic. Returning `nil` is ambiguous because `nil` may also be a valid value. It also lacks information pertaining to the nature of the error. Raising an exception is both expensive and usurps the normal flow of control. All of these problems can be solved with the use of a `Maybe`.

A ‘Maybe` is unambiguous with regard to whether or not it contains a value. When `Just` it contains a value, when `Nothing` it does not. When `Just` the value it contains may be `nil`, which is perfectly valid. When `Nothing` the reason for the lack of a value is contained as well. The previous Erlang example can be duplicated in Ruby in a principled way by having functions return `Maybe` objects:

“‘ruby result = MyFileUtils.consult(“data.dat”) # returns a Maybe if result.just?

do_something_useful(result.value)      # or result.just

else

logger.error(result.reason)            # or result.nothing

end “‘

@example Returning a Maybe from a Function

module MyFileUtils
  def self.consult(path)
    file = File.open(path, 'r')
    Concurrent::Maybe.just(file.read)
  rescue => ex
    return Concurrent::Maybe.nothing(ex)
  ensure
    file.close if file
  end
end

maybe = MyFileUtils.consult('bogus.file')
maybe.just?    #=> false
maybe.nothing? #=> true
maybe.reason   #=> #<Errno::ENOENT: No such file or directory @ rb_sysopen - bogus.file>

maybe = MyFileUtils.consult('README.md')
maybe.just?    #=> true
maybe.nothing? #=> false
maybe.value    #=> "# Concurrent Ruby\n[![Gem Version..."

@example Using Maybe with a Block

result = Concurrent::Maybe.from do
  Client.find(10) # Client is an ActiveRecord model
end

# -- if the record was found
result.just? #=> true
result.value #=> #<Client id: 10, first_name: "Ryan">

# -- if the record was not found
result.just?  #=> false
result.reason #=> ActiveRecord::RecordNotFound

@example Using Maybe with the Null Object Pattern

# In a Rails controller...
result = ClientService.new(10).find    # returns a Maybe
render json: result.or(NullClient.new)

@see hackage.haskell.org/package/base-4.2.0.1/docs/Data-Maybe.html Haskell Data.Maybe @see github.com/purescript/purescript-maybe/blob/master/docs/Data.Maybe.md PureScript Data.Maybe