dry-rb / dry-types

Flexible type system for Ruby with coercions and constraints
https://dry-rb.org/gems/dry-types
MIT License
861 stars 134 forks source link

default causes try/1 to raise an ConstraintError exception rather than an Failure #469

Open krainboltgreene opened 9 months ago

krainboltgreene commented 9 months ago

Describe the bug

default causes try/1 to raise an ConstraintError exception rather than an Failure. This seems completely different from every other result of try and the documentation doesn't detail this fact.

To Reproduce

Types::String.default{"x"}.try(1)
# Dry::Types::ConstraintError: 1 violates constraints (type?(String, 1) failed)
from /Users/krainboltgreene/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/dry-types-1.7.1/lib/dry/types/constrained.rb
Types::String.default("x".freeze).try(1)
# => Dry::Types::ConstraintError: 1 violates constraints (type?(String, 1) failed)
from /Users/krainboltgreene/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/dry-types-1.7.1/lib/dry/types/constrained.rb

Expected behavior

Types::String.default("x".freeze).try("")
# => #<Dry::Types::Result::Success input="">
Types::String.constrained(format: /a/i).try(1)
# => #<Dry::Types::Result::Failure input=1 error=#<Dry::Types::ConstraintError: 1 violates constraints (type?(String, 1) AND format?(/a/i, 1) failed)>>
Types::String.try(1)
# => #<Dry::Types::Result::Failure input=1 error=#<Dry::Types::ConstraintError: 1 violates constraints (type?(String, 1) failed)>>

My environment