简体   繁体   中英

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM