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

Generate fromProduct in the companion object (Scala 3) #3

Closed
sideeffffect opened this issue Feb 9, 2024 · 1 comment
Closed

Generate fromProduct in the companion object (Scala 3) #3

sideeffffect opened this issue Feb 9, 2024 · 1 comment

Comments

@sideeffffect
Copy link

sideeffffect commented Feb 9, 2024

In Scala 3, there exists a method fromProduct that gets automatically generated in case classes' companion objects. For backwards compatibility, this methods needs to be explicitly re-implemented and needs to accept tuples of all the necessary arities.

The generated should look something like this

def fromProduct(p: Product): Person = p.productArity match
  case 2 =>
    Person(
      p.productElement(0).asInstanceOf[...],
      p.productElement(1).asInstanceOf[...],
    )
  case 3 =>
    Person(
      p.productElement(0).asInstanceOf[...],
      p.productElement(1).asInstanceOf[...],
      p.productElement(2).asInstanceOf[...],
    )
  ...

Context:

/cc @armanbilge

@sideeffffect sideeffffect changed the title Generate fromProduct in the companion object Generate fromProduct in the companion object (Scala 3) Feb 9, 2024
@lihaoyi
Copy link
Member

lihaoyi commented Feb 11, 2024

Done

@lihaoyi lihaoyi closed this as completed Feb 11, 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

2 participants