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

Tagged unions with custom serializers not working #394

Closed
edwardcwang opened this issue Jun 22, 2022 · 1 comment · Fixed by #602
Closed

Tagged unions with custom serializers not working #394

edwardcwang opened this issue Jun 22, 2022 · 1 comment · Fixed by #602

Comments

@edwardcwang
Copy link

Scala 3.1.1, upickle 2.0.0

import upickle.default._

sealed trait BooleanOrInt
object BooleanOrInt {
  implicit val rw: upickle.default.ReadWriter[BooleanOrInt] = upickle.default.macroRW
}
case class IsBoolean(val value: Boolean) extends BooleanOrInt
object IsBoolean {
  implicit val rw: upickle.default.ReadWriter[IsBoolean] = upickle.default.readwriter[Boolean].bimap[IsBoolean](_.value, IsBoolean(_))
}
case class IsInt(val value: Int) extends BooleanOrInt
object IsInt {
  implicit val rw: upickle.default.ReadWriter[IsInt] = upickle.default.readwriter[Int].bimap[IsInt](_.value, IsInt(_))
}

val x: BooleanOrInt = IsBoolean(false); upickle.default.write(x)

Stacktrace:

java.lang.ClassCastException: class upickle.core.Types$$anon$1 cannot be cast to class upickle.core.Types$TaggedWriter (upickle.core.Types$$anon$1 and upickle.core.Types$TaggedWriter are in unnamed module of loader 'app')
  upickle.core.Types.upickle$core$Types$$scanChildren(Types.scala:323)
  upickle.core.Types$TaggedWriter$Node.findWriter(Types.scala:361)
  upickle.core.Types$$anon$2.findWriter(Types.scala:50)
  upickle.core.Types$TaggedWriter.write0(Types.scala:348)
  upickle.core.Types$TaggedWriter.write0$(Types.scala:345)
  upickle.core.Types$$anon$2.write0(Types.scala:47)
  upickle.core.Types$Writer.write(Types.scala:130)
  upickle.core.Types$Writer.write$(Types.scala:118)
  upickle.core.Types$$anon$2.write(Types.scala:47)
  upickle.core.Types$Writer.transform(Types.scala:126)
  upickle.core.Types$Writer.transform$(Types.scala:118)
  upickle.core.Types$$anon$2.transform(Types.scala:47)
  upickle.Api$transform.transform(Api.scala:124)
  upickle.Api$transform.to(Api.scala:125)
  upickle.Api.write(Api.scala:48)
  upickle.Api.write$(Api.scala:17)
  upickle.default$.write(Api.scala:157)
@lihaoyi
Copy link
Member

lihaoyi commented Feb 27, 2023

This does not work in Scala 2 as well. The issue seems to be that macroRW expects all subclasses to be macroRW as well, and does not play nicely with the custom bimaped readwriters. A workaround could be to make BooleanOrInt.rw a custom readwriter as well

@lihaoyi lihaoyi changed the title Tagged unions not working in Scala 3 Tagged unions with custom serializers not working Feb 27, 2023
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 a pull request may close this issue.

2 participants