-
-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document how sum types works with structs #159
Comments
For what it's worth, this was very unexpected behavior to me: class A < Dry::Struct
attribute :id, Types::Strict::Integer
attribute :type, Types::Strict::String
attribute :name, Types::Strict::String
end
class B < Dry::Struct
attribute :id, Types::Strict::Integer
attribute :type, Types::Strict::String
attribute :name, Types::Strict::String
attribute :primary_function, Types::Strict::String.optional
end
class WithSum < Dry::Struct
attribute :sum, A | B
end
WithSum.new(
sum: B.new(
id: 123,
type: 'foo',
name: 'bar',
primary_function: 'yo'
)
) # => #<WithSum sum=#<A id=123 type="foo" name="bar">> I expected it to return my instance of A nice first step, would be to at least document how sum types work. But I wonder if it's intended to work this way too. |
The problem lies in implicit coercion with class StrictStruct < Dry::Struct
schema schema.strict
end
class A < StrictStruct
attribute :id, Types::Strict::Integer
attribute :type, Types::Strict::String
attribute :name, Types::Strict::String
end
class B < StrictStruct
attribute :id, Types::Strict::Integer
attribute :type, Types::Strict::String
attribute :name, Types::Strict::String
attribute :primary_function, Types::Strict::String.optional
end
class WithSum < Dry::Struct
attribute :sum, A | B
end This will still coerce data internally but these attempts will fail. I think we'll remove |
Thank you for the context @flash-gordon 🙏. And thanks for the suggestion. I solved our case with constrained types:
|
Yeah, I do something similar to discriminate variants. 👍 |
refs #158
The text was updated successfully, but these errors were encountered: