# Default values and #try

**URL:** <https://discourse.dry-rb.org/t/default-values-and-try/1817>\
**Category:** dry-types\
**Created:** [July 24, 2024, 1:24pm UTC](https://discourse.dry-rb.org/t/default-values-and-try/1817 "2024-07-24T13:24:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elcuervo](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/elcuervo/32/900_2.png) [@elcuervo](https://discourse.dry-rb.org/u/elcuervo)\
**Post date:** [July 24, 2024, 1:24pm UTC](https://discourse.dry-rb.org/t/default-values-and-try/1817/1 "2024-07-24T13:24:15Z")

</div>

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`.

```ruby
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)

```

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/alassek/32/314_2.png) [@alassek](https://discourse.dry-rb.org/u/alassek)\
**Post date:** [July 24, 2024, 4:07pm UTC](https://discourse.dry-rb.org/t/default-values-and-try/1817/2 "2024-07-24T16:07:10Z")

</div>

Yes, this appears to be a bug. There [is an open issue](https://github.com/dry-rb/dry-types/issues/469) for it.

---

<div class="post-metadata">

**Author:** ![elcuervo](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/elcuervo/32/900_2.png) [@elcuervo](https://discourse.dry-rb.org/u/elcuervo)\
**Post date:** [July 24, 2024, 6:56pm UTC](https://discourse.dry-rb.org/t/default-values-and-try/1817/3 "2024-07-24T18:56:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![elcuervo](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/elcuervo/32/900_2.png) [@elcuervo](https://discourse.dry-rb.org/u/elcuervo)\
**Post date:** [July 24, 2024, 7:41pm UTC](https://discourse.dry-rb.org/t/default-values-and-try/1817/4 "2024-07-24T19:41:19Z")

</div>

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

> <https://github.com/dry-rb/dry-types/pull/475>
>
> Fixes instances when a Default type runs \`#try\` with a failing value.
> 
> Closes …#469
> 
> \`\`\`ruby
> Types::Integer.default(1).try("a")
> \# =\> #\<Dry::Types::Result::Failure input="a" error=#\<Dry::Types::ConstraintError: "a" violates constraints (type?(Integer, "a") failed)\>\>
> \`\`\`
