简体   繁体   中英

Match parameterized type using Scala's pattern matching

I have a class Operator[T: TypeTag : ClassTag] and in of it's methods, I want to use Scala's pattern matching on the parameterized type T . I know how pattern matching works and so tried the following in Scala's repl and got an error.

scala> def matchTest() = T match {
     | case x: String => "abcd"
     | case _ => null
     | }
<console>:11: error: not found: value T
       def matchTest() = T match {
                            ^

T is a type, not a value. You can only match on values.

You can get an implicitly[ClassTag[T]] or implicitly[TypeTag[T]] , which are values, and do matching on those.

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