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

Case class deserialization doesn't go through apply in Scala 3 #552

Open
smarter opened this issue Jan 29, 2024 · 0 comments
Open

Case class deserialization doesn't go through apply in Scala 3 #552

smarter opened this issue Jan 29, 2024 · 0 comments

Comments

@smarter
Copy link

smarter commented Jan 29, 2024

https://com-lihaoyi.github.io/upickle/#Basics says:

Case classes are serialized using the apply and unapply methods on their companion objects.

This isn't the case in Scala 3:

case class Foo(x: Int) derives ReadWriter
object Foo:
  def apply(x: Int): Foo =
    assert(x > 0)
    new Foo(x)
@main def Test =
  read[Foo]("""{"x":-1}""") // Successfully constructs a Foo.

This is because deserialization uses Mirror#fromProduct which just calls the constructor:

override def fromProduct(p: Product): T = m.fromProduct(p)

@smarter smarter changed the title Case class serialization doesn't go through apply in Scala 3 Case class deserialization doesn't go through apply in Scala 3 Jan 29, 2024
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

1 participant