开发者

Pattern matching on Class[_] type?

I'm trying to use Scala pattern matching on Java Class[_] (in context of using Java reflection from Scala) but I'm getting some unex开发者_JAVA技巧pected error. The following gives "unreachable code" on the line with case jLong

def foo[T](paramType: Class[_]): Unit = {
  val jInteger = classOf[java.lang.Integer]
  val jLong = classOf[java.lang.Long]
  paramType match {
    case jInteger => println("int")
    case jLong => println("long")
  }
}

Any ideas why this is happening ?


The code works as expected if you change the variable names to upper case (or surround them with backticks in the pattern):

scala> def foo[T](paramType: Class[_]): Unit = {
     |   val jInteger = classOf[java.lang.Integer]
     |   val jLong = classOf[java.lang.Long]
     |   paramType match {
     |     case `jInteger` => println("int")
     |     case `jLong` => println("long")
     |   }
     | }
foo: [T](paramType: Class[_])Unit

scala> foo(classOf[java.lang.Integer])
int

In your code the jInteger in the first pattern is a new variable—it's not the jInteger from the surrounding scope. From the specification:

8.1.1 Variable Patterns

... A variable pattern x is a simple identifier which starts with a lower case letter. It matches any value, and binds the variable name to that value.

...

8.1.5 Stable Identifier Patterns

... To resolve the syntactic overlap with a variable pattern, a stable identifier pattern may not be a simple name starting with a lower-case letter. However, it is possible to enclose a such a variable name in backquotes; then it is treated as a stable identifier pattern.

See this question for more information.


On your pattern matching, each of these 2 cases try to create place holder names instead of matching the class type as expected.

If you use upper case in the starting character, you'll be fine

def foo[T](paramType: Class[_]): Unit = {
  val JInteger = classOf[Int]
  val JLong = classOf[Long]
  paramType match {
    case JInteger => println("int")
    case JLong => println("long")
  }
}

scala> foo(1.getClass)
int


JMPL is simple java library, which could emulate some of the features pattern matching, using Java 8 features.

    matches(data).as(
      Integer.class, i  -> { System.out.println(i * i); },
      Byte.class,    b  -> { System.out.println(b * b); },
      Long.class,    l  -> { System.out.println(l * l); },
      String.class,  s  -> { System.out.println(s * s); },
      Null.class,    () -> { System.out.println("Null value "); },
      Else.class,    () -> { System.out.println("Default value: " + data); }
   );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜