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 Scala 3 Union Types #752

Open
robmwalsh opened this issue Oct 14, 2022 · 4 comments
Open

Support Scala 3 Union Types #752

robmwalsh opened this issue Oct 14, 2022 · 4 comments

Comments

@robmwalsh
Copy link

Scala 3 union types aren't supported, but unions are very common in typescript - support for this would be super handy!

https://scastie.scala-lang.org/1h8VItvQQdiuZpGR4EX0qg

@Iltotore
Copy link

This is unfortunately a limitation of Scala due to union type commutativity.

Taking your example:

given [A, B](using aCodec: JsonCodec[A], bCodec: JsonCodec[B]): JsonCodec[A | B] =
  aCodec.asInstanceOf[JsonCodec[A | B]] <> bCodec.asInstanceOf[JsonCodec[A | B]]

sealed case class Test(
    id: Int | String | Boolean
) derives JsonCodec
//Ambiguous implicit because Int | String | Boolean =:= Int | Boolean | String =:= String | Int | Boolean =:= ...

(Scastie)

This limitation is discussed in Scala Contributors

@robmwalsh
Copy link
Author

robmwalsh commented Oct 27, 2022

I found this after I created this issue https://github.com/iRevive/union-derivation I'm not sure what the policy on external library dependencies are here but I think it's small enough and low enough effort to perhaps just subsume it into zio-json like izumi reflect ended up in zio.

edit: perhaps this should go into zio instead - the whole ecosystem would benefit then. I'll create an issue there instead, but will leave this open as there will probably be some minimal work to do once it's in zio.

@liewhite
Copy link

Implemented by macros here. For reference only
liewhite/json

@Iltotore
Copy link

Iltotore commented Nov 19, 2022

Here is another example using a more "mirror-like" mechanism: https://gist.github.com/Iltotore/eece20188d383f7aee16a0b89eeb887f/#file-ziojson-sc

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

No branches or pull requests

3 participants