Default values and #try

EDIT: As correctly pointed by @alassek this is related to a known issue with #try and Default

I was expecting to be able to use to_monad on a type with default values.
Is this an expected behavior or am I missing something?

Success seems to be wrapped correctly but not Failure.

require "dry/types"

Dry::Types.load_extensions(:monads)

Types = Dry.Types()

One = Types::Symbol.enum(*%i(a b c))
Two = Types::Symbol.default(:c).enum(*%i(c d e))

One.try(:w).to_monad
# => Failure([#<Dry::Types::ConstraintError: :w violates constraints (included_in?([:c, :d, :e], :w) failed)>, :w])

Two.try(:d).to_monad
# => Success(:d)

Two.try(:w).to_monad
# Dry::Types::ConstraintError: :w violates constraints (included_in?([:c, :d, :e], :w) failed)

Yes, this appears to be a bug. There is an open issue for it.

Checked the forum but not the Issues. My bad.
Thanks for the reply!

I don’t know if this is the expected way to solve this issue but this seems to do the trick.

1 Like