Skip to content
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

Support arbitrary-sized unions with N-ary sum type #168

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

fieldstrength
Copy link

Hi there, I've been interested to have this feature for some time, since in my company we've frequently needed to add to the EitherN module whenever we add something. So I've taken a stab at implementing it. I used a sum type similar to NS from generics-sop (but specialized to not be kind-polymorphic).

Since this is a larger change I'm of course happy to hear the perspective of the maintainers and users and try to accommodate your preferences.

With the existing round-trip tests Im fairly confident the implementation is correct. I also ran another test to check the consistency with the existing implementation, which adds to my confidence. (That test is not included in this patch, because it removes the old types).

I'm not sure how well covered the unions are by the benchmark suite. I think the performance should at least not be too much different, but Im also interested to validate that and make adjustments if necessary. However, I wanted to start the conversation here first at least, to make sure there is buy-in for this change before going further.

@@ -464,22 +466,17 @@ mkFieldTypeName namespaceBehavior = \case
S.Enum n _ _ _ -> [t| $(conT $ mkDataTypeName namespaceBehavior n)|]
where go = mkFieldTypeName namespaceBehavior
union = \case
[] ->
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if anyone would take issue with this, but I'm personally very pleased to be ride of this special case. I was recently involved in designing a new schema and found it was inconvenient that Avro generation broke because of types whose values had simply not been defined yet, since Void ~ NS [] is a perfectly valid type, and it doesn't hurt anything to have such a type around while you're developing a schema, whereas fake or provisional values can be troublesome.

@AlexeyRaga
Copy link
Member

AlexeyRaga commented Dec 26, 2021

This looks good, thanks!

Pme question: what would be the Haskell type reported to :t or by HIE for such generated sum types?

And could you please add some Readme section or an example/test about how to use / pattern match on N-sums?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants