Kevin Meredith
Kevin Meredith

Reputation: 41919

Understanding `apply` and `unapply` with Singleton Type

Given:

import shapeless._
import syntax.singleton._

case class Foo(a: String, one: Witness.`1`.T)

Why do Foo.apply _ and Foo.unapply _ have different signatures, namely the Int(1) versus Int, respectively?

scala> Foo.apply _
res5: (String, Int(1)) => Foo = <function2>

scala> Foo.unapply _
res6: Foo => Option[(String, Int)] = <function1>

Upvotes: 8

Views: 256

Answers (1)

HTNW
HTNW

Reputation: 29193

As far as I can tell, in scala.tools.nsc.typechecker.Namers (compiler code), apply used to be generated by completerOf and caseApplyMethodCompleter, while unapply used to come from just completerOf. This and probably other factors, likely some subtle bugs, causes unapply to lose type information. This change refactors the code a bit and results in both methods going through the new applyUnapplyMethodCompleter, which fixes it. Since there doesn't appear to be any ticket on the issue, the true cause will take some extra digging. It's probably safe to assume that this wasn't intended behavior.

Upvotes: 6

Related Questions