milessabin / shapeless

Generic programming for Scala

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Could not find Generic instance if object has explicit "unapply" method since Scala 2.13.4

xuwei-k opened this issue · comments

A.scala

package example

case class A(x: Int, y: String)

object A {
  def unapply(a: A): Option[(Int, String, String)] = ???
}

object Main {
  shapeless.Generic[A]
}

build.sbt

libraryDependencies += "com.chuusai" %% "shapeless" % "2.3.3"

scalaVersion := "2.13.4"

log

A.scala:10:20: could not find implicit value for parameter gen: shapeless.Generic[example.A]
[error]   shapeless.Generic[A]
[error]                    ^
[error] one error found
[error] (Compile / compileIncremental) Compilation failed

Does it work with 2.13.3? 🤔
I would not expect it to work because the unapply signature does not match the apply:

object A {
  def apply(x: Int, y: String): A = ??? // generated
  def unapply(a: A): Option[(Int, String, String)] = ???
}

There has been a change in Scala - I don't know if intentional or not:

This compiles fine on 2.13.3

object Test {
  case class A(x: Int, y: String)
  object A {
    def unapply(a: A): Option[(Int, String, String)] = ???
  }

  def foo(a: A) = a match {
    case A(x, y) => (x, y)
  }
}

And fails since 2.13.4:

not enough patterns for object A offering (Int, String, String): expected 3, found 2
[error]     case A(x, y) => (x, y)
[error]          ^
[error] one error found

We could probably work around this change but it's harder than it looks