Skip to content

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

Closed
@sideeffffect

Description

@sideeffffect

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions