Scala 2.13 No implicit view available from java.util.Map[String,Double] => scala.collection.IterableOnce[B]

69 Views Asked by At

I migrate some scala code from 2.12 to 2.13 and I have the following code

def getMetrics(): java.util.Map[String, Double] ={
    transformers.map{
      case transformer => transformer match{
        case t: EvaluationTransformFunction => t.getMetric.asJava
      }
    }.flatten.toMap.asJava
  }

that produces the error

error: No implicit view available from java.util.Map[String,Double] => scala.collection.IterableOnce[B].
[ERROR]     }.flatten.toMap.asJava
[ERROR]       ^

can you explain what is the error about?

1

There are 1 best solutions below

1
Gaël J On

We're missing a bit more info about the exact types you are manipulating but the idea is that:

  • the type of transformers.map(...) is a Iterable[SomeJavaCollectionType]
  • flatten works only if SomeJavaCollectionType can be "transformers/"viewed" as a Iterable[SomeOtherType] so that Iterable[SomeJavaCollectionType] can be viewed as Iterable[Iterable[SomeOtherType]] and thus flattened
  • there's no such "implicit view" available for your actual SomeJavaCollectionType which seems to be JavaMap[String, Double] if I read it correctly

I'm slightly surprised it was working in Scala 2.12. Maybe there's an import that would make it work in Scala 2.13 as well (i.e. bring the "implicit view" in scope).

Anyway, I would just get rid of the inner asJava and only convert to Java collection at the end of your method:

def getMetrics(): java.util.Map[String, Double] ={
    transformers.map{
      case transformer => transformer match{
        case t: EvaluationTransformFunction => t.getMetric
      }
    }.flatten.toMap.asJava
  }