DEV Community

Discussion on: Modelling domain with state machines in ReasonML

 
austindd profile image
Austin • Edited

That's a pretty good description of the way variants work. OCaml/Reason does not have a built-in Either type, but they do have this "disjoint tagged unions" which can behave the same way.

The most common data type that models the Either type you describe (used in Haskell) is the Result type. It is modeled using the variant constructors Ok and Error. So the Reason code would look like:

module Result = {
  type t('a, 'e) =
    | Ok('a)
    | Error('e);
};

And the type we have there is a bifunctor and a monad, just like Either.