com-lihaoyi / unroll

23 stars 0 forks source link

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

Closed sideeffffect closed 6 months ago

sideeffffect commented 6 months ago

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

lihaoyi commented 6 months ago

Done