dry-types

v1.8 v1.7
  1. Introduction
  2. Getting Started
  3. Built-in Types
  4. Type Attributes
  5. Default Values
  6. Fallbacks
  7. Sum
  8. Constraints
  9. Hash Schemas
  10. Array With Member
  11. Enum
  12. Map
  13. Custom Types
  14. Custom Type Builders
  15. Extensions
    1. Maybe
    2. Monads

TOC


Sum

You can specify sum types using | operator, it is an explicit way of defining what the valid types of a value are.

For example dry-types defines the Bool type which is a sum consisting of the True and False types, expressed as Types::True | Types::False.

Another common case is defining that something can be either nil or something else:

nil_or_string = Types::Nil | Types::String

nil_or_string[nil] # => nil
nil_or_string["hello"] # => "hello"

nil_or_string[123] # raises Dry::Types::ConstraintError