cscan cscan - 2 months ago 16
Scala Question

Scala: Match based on expression

I have some scala code with which I'd like to use scala's pattern matching. What the code does is get a class's public getters and add the method name as the json key and the method value as the json value (serializing it if it's an object or array). Here's the code:

private def serialize(any: Any): JsonObject = {
val json = new JsonObject()
val rm = scala.reflect.runtime.currentMirror
val accessors = rm.classSymbol(any.getClass).toType.members.collect {
case m: MethodSymbol if m.isGetter && m.isPublic => m
}
val instanceMirror = rm.reflect(any)
for (accessor <- accessors) {
val key = properCase(accessor.name.toString)
val value = instanceMirror.reflectMethod(accessor).apply()
if (accessor.returnType <:< typeOf[String])
json.addProperty(key, value.asInstanceOf[String])
else if (accessor.returnType <:< typeOf[Character])
json.addProperty(key, value.asInstanceOf[Character])
else if (accessor.returnType <:< typeOf[Boolean])
json.addProperty(key, value.asInstanceOf[Boolean])
else if (accessor.returnType <:< typeOf[Number])
json.addProperty(key, value.asInstanceOf[Number])
else if (accessor.returnType <:< typeOf[Iterable[Any]])
json.add(key, serialize(value.asInstanceOf[Iterable[Any]]))
else
json.add(key, serialize(value.asInstanceOf[Any]))
}
json
}


I'd like to do something like this:

accessors match {
case _.returnType <:< typeOf[String] => json.addProperty(
properCase(key.name.toString),
instanceMirror.reflectMethod(accessor).apply().asInstanceOf[String])
...
}


However, the compiler is very unhappy with that, saying that
'=>' expected but '.' found
. I assume that the compiler isn't expecting an expression in the match statement and wants a constant (just as in java). Is there any way to use scala's match style syntax with an expression?

Answer

To have a test in pattern-matching, write

case x if x.returnType <:< typeOf[String] => ...