Scala – how to pattern match when chaining two implicit conversions?

  • A+
Category:Languages

I wrote a method to parse Metrics data and at first faced a problem with the type of transactionMap which is a java.util.Map. And I solved it using JavaConverters.

def parseMetrics(metric: Metric) = {     import scala.collection.JavaConverters._     metric.transactionMap.asScala.values.map {       case false => "N"        case true => "Y"     }.toList 

But after that I got an error while pattern matching true and false values: pattern type is incompatible with expected type, found: Boolean, required: java.lang.Boolean

As far as I understand Scala does not chain two implicit conversions. Is there a way to fix it using JavaConverters?

 


Use if/else rather than a match statement for Boolean checking:

def parseMetrics(metric: Metric) = {     import scala.collection.JavaConverters._     metric.transactionMap.asScala.values.map {       x => if (x) "Y" else "N"     }.toList 

My suspicion is that within the if statement the java.lang.Boolean (which I presume x is here) can be coerced to Boolean via import scala.collection.JavaConverters._... but the match statement doesn't do the same coercion, but would have to be made explicitly (or match on the java.lang.Boolean values).

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: