Maybe
The dry-monads gem provides an approach to handling optional values by returning a Monad object. This allows you to pass your type to a Maybe(x)
block that only executes if x
returns Some
or None
.
NOTE: Requires the dry-monads gem to be loaded.
- Load the
:maybe
extension in your application.
require 'dry-types'
Dry::Types.load_extensions(:maybe)
module Types
include Dry.Types()
end
- Append
.maybe
to a Type to return a Monad object
x = Types::Maybe::Strict::Integer[nil]
Maybe(x) { puts(x) }
x = Types::Maybe::Coercible::String[nil]
Maybe(x) { puts(x) }
x = Types::Maybe::Strict::Integer[123]
Maybe(x) { puts(x) }
x = Types::Maybe::Strict::String[123]
Maybe(x) { puts(x) }
Types::Maybe::Strict::Integer[nil] # None
Types::Maybe::Strict::Integer[123] # Some(123)
Types::Maybe::Coercible::Float[nil] # None
Types::Maybe::Coercible::Float['12.3'] # Some(12.3)
# 'Maybe' types can also accessed by calling '.maybe' on a regular type:
Types::Strict::Integer.maybe # equivalent to Types::Maybe::Strict::Integer
You can define your own optional types:
maybe_string = Types::Strict::String.maybe
maybe_string[nil]
# => None
maybe_string[nil].fmap(&:upcase)
# => None
maybe_string['something']
# => Some('something')
maybe_string['something'].fmap(&:upcase)
# => Some('SOMETHING')
maybe_string['something'].fmap(&:upcase).value_or('NOTHING')
# => "SOMETHING"