dry-monads

v1.8
  1. Introduction
  2. Getting started
  3. Maybe
  4. Result
  5. Do notation
  6. Try
  7. List
  8. Task
  9. Validated
  10. Case equality
  11. Tracing failures
  12. Pattern matching
  13. Unit

TOC

  1. Discarding values

Unit

Some constructors do not require you to pass a value. As a default they use Unit, a special singleton value:

extend Dry::Monads[:result]

Success().value! # => Unit

Unit doesn't have any special properties or methods, it's similar to nil except for it is not i.e. if Unit passes.

Unit is usually excluded from the output:

extend Dry::Monads[:result]

# Outputs as "Success()" but technically it's "Success(Unit)"
Success()

Discarding values

When the outcome of an operation is not a caller's concern, call .discard, it will map the wrapped value to Unit:

extend Dry::Monads[:result]

result = create_user # returns Success(#<User...>) or Failure(...)

result.discard # => Maps Success(#<User ...>) to Success() but lefts Failure(...) intact