0

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?

Gaël J
  • 11,274
  • 4
  • 17
  • 32

1 Answers1

2

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
  }
Gaël J
  • 11,274
  • 4
  • 17
  • 32
  • Oh. This question is similar to [Scala 2.13 error: overloaded method w with alternatives](https://stackoverflow.com/questions/76872361/scala-2-13-error-overloaded-method-w-with-alternatives). My answer about [how to migrate collections to scala 2.13](https://stackoverflow.com/a/76872849/7214091), might help to complement this answer. – Gastón Schabas Aug 10 '23 at 05:30