15
votes

Can't I use a generic on the unapply method of an extractor along with an implicit "converter" to support a pattern match specific to the parameterised type?

I'd like to do this (Note the use of [T] on the unapply line),

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)
}

But I get the following compilation error

Error: (25, 10) not found: type ExampleExtractor case ExampleExtractor[String] (x) => x ^

It works fine if I have only one implicit val in scope and drop the type hint (see below), but that defeats the object.

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)
}
1
I don't think this is possible at the moment, see SI-884.Peter Neyens

1 Answers

3
votes

A variant of your typed string decoder looks promising:

trait StringDecoder[A] { 
   def fromString(s: String): Option[A] 
}

class ExampleExtractor[T](ev: StringDecoder[T]) {
   def unapply(s: String) = ev.fromString(s)
}
object ExampleExtractor { 
   def apply[A](implicit ev: StringDecoder[A]) = new ExampleExtractor(ev) 
}

then

implicit val intDecoder = new StringDecoder[Int] { 
    def fromString(s: String) = scala.util.Try {
        Integer.parseInt(s)
    }.toOption
}

val asInt = ExampleExtractor[Int]
val asInt(Nb) = "1111"

seems to produce what you're asking for. One problem remains: it seems that trying to

val ExampleExtractor[Int](nB) = "1111"

results in a compiler crash (at least inside my 2.10.3 SBT Scala console).