-
-
Notifications
You must be signed in to change notification settings - Fork 92
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
How to implement union types / List[Either[A,B]] in Scala 2.13 ? #1139
Comments
Hi Dominik, Unfortunately, the current version of But you can write a custom codec that would be suitable for you case. The main challenge here is how to distinguish different types of
|
Hello!
I have a endpoint that streams a json-array (via akka json-streaming) (array, starting with
[
, then the elements, then]
)Usually, the stream returns elements of Type
A
(happy path). But the stream could also fail at one point and then return a singleB
as the last element and then the closing]
.I'm using Scala 2.13.
In Scala 3 I could use a union type to express this as
List[A | B]
..However, I'm currently stuck with scala 2.13, so as I cannot make
A
andB
have the same supertype,I thought about encoding this as a
List[Either[A,B]]
but this expects that the elements thenhave a
type
field, e.g. its serializing theEither
and I'm getting this error:I created a small sample which would encode this and expectedly its encoding the either like this:
returns this json
Is there any way to get the union type functionality in scala 2.13 ? e.g. a codec that first tries the
JsonValueCodec[A]
and if that fails triesJsonValueCodec[B]
?Thanks!
The text was updated successfully, but these errors were encountered: