Nie mogę użyć generycznego w metodzie unapply
ekstraktora wraz z niejawnym "konwerterem" do obsługi dopasowania wzorca określonego sparametryzowanego typu?Czy "ekstraktor" Scala może używać generycznych na Unapply?
Chciałbym zrobić (Zauważ użycie [T]
na linii unapply
),
trait StringDecoder[A] {
def fromString(string: String): Option[A]
}
object ExampleExtractor {
def unapply[T](a: String)(implicit evidence: StringDecoder[T]): Option[T] = {
evidence.fromString(a)
}
}
object Example extends App {
implicit val stringDecoder = new StringDecoder[String] {
def fromString(string: String): Option[String] = Some(string)
}
implicit val intDecoder = new StringDecoder[Int] {
def fromString(string: String): Option[Int] = Some(string.charAt(0).toInt)
}
val result = "hello" match {
case ExampleExtractor[String](x) => x // <- type hint barfs
}
println(result)
}
Ale pojawia się następujący błąd kompilacji
Error: (25, 10) not found: type ExampleExtractor case ExampleExtractor[String] (x) => x ^
Działa dobrze, jeśli mam tylko jedno niejawne val
w zasięgu i upuszczam wskazówkę typu (patrz poniżej), ale to pokonuje obiekt.
object Example extends App {
implicit val intDecoder = new StringDecoder[Int] {
def fromString(string: String): Option[Int] = Some(string.charAt(0).toInt)
}
val result = "hello" match {
case ExampleExtractor(x) => x
}
println(result)
}
Nie sądzę, że jest to obecnie możliwe, patrz [SI-884] (https://issues.scala-lang.org/browse/SI-884). –