score:10

Accepted answer

the compiler automatically translates your code roughly to this:

  def get[t](list: list[any])(implicit tag: classtag[t]) = list.flatmap {
    case (element @ tag(_: t)) => some(element)
    case _ => none
  }

classtag has an unapply(x: any) overload that allows it to pattern match on values. i've cleaned up the tree obtained from reify to only show the relevant parts, but this will show you the full tree:

scala.reflect.runtime.universe.reify { 
    def get[t](list: list[any])(implicit tag: classtag[t]) = {
      list.flatmap {
        case element: t => some(element)
        case _ => none
      }
    }
}

also see the scaladoc:

the compiler tries to turn unchecked type tests in pattern matches into checked ones by wrapping a (_: t) type pattern as ct(_: t), where ct is the classtag[t] instance. type tests necessary before calling other extractors are treated similarly. someextractor(...) is turned into ct(someextractor(...)) if t in someextractor.unapply(x: t) is uncheckable, but we have an instance of classtag[t].


Related Query

More Query from same tag