Skip to content

Cannot do a map on a Map with Scala 3, with Scala 2.13 it works ok #21367

Open
@DavidPerezIngeniero

Description

@DavidPerezIngeniero

Compiler version

  • Java 21
  • Scala 3.3.3 (and 3.4.2)

Minimized code

This interactive scala-cli session shows the problem:

❯ scala-cli -j system --dep org.python:jython-standalone:2.7.1b3 -S 3.3.3
Welcome to Scala 3.3.3 (21.0.3, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

cala> import org.python.core.*
                                                                                                                                          
scala> import scala.jdk.CollectionConverters.*
                                                                                                                                          
scala> val p = new PyDictionary
val p: org.python.core.PyDictionary = {}

scala> p.asScala
-- [E008] Not Found Error: -----------------------------------------------------
1 |p.asScala
  |^^^^^^^^^
  |value asScala is not a member of org.python.core.PyDictionary
1 error found

cala> (p: java.util.Map[?, ?]).asScala
val res0: scala.collection.mutable.Map[?, ?] = Map()
                                                                                                                                          
scala> (p: java.util.Map[Any, Any]).asScala.map { case (k,v) => (k, v) }
-- [E007] Type Mismatch Error: -------------------------------------------------
1 |(p: java.util.Map[Any, Any]).asScala.map { case (k,v) => (k, v) }
  | ^
  | Found:    (p : org.python.core.PyDictionary)
  | Required: java.util.Map[Any, Any]
  |
  | longer explanation available when compiling with `-explain`
1 error found

Comparison with Scala 2.13.14:

 scala-cli -j system --dep org.python:jython-standalone:2.7.1b3 -S 2.13.14
Welcome to Scala 2.13.14 (OpenJDK 64-Bit Server VM, Java 21.0.3).
Type in expressions for evaluation. Or try :help.

scala> import org.python.core._
import org.python.core._

scala> import scala.jdk.CollectionConverters._
import scala.jdk.CollectionConverters._

scala> val p = new PyDictionary
val p: org.python.core.PyDictionary = {}

scala> p.asScala
val res0: scala.collection.concurrent.Map[_, _] = Map()

scala> p.asScala.map{case(k,v) => (k,v) }
val res1: scala.collection.mutable.Map[Object,Object] = HashMap()

Output

Error compiles

Expectation

Code does compile o error message can be unsderstood.
With Scala 2.13.14 it compiles.

In addition, with Scala 2.13 I don't need a type adscription to Map.

Notes:

PyDictionary is defined like this:

public class PyDictionary extends AbstractDict implements ConcurrentMap, Traverseproc {
   ...
}
public interface ConcurrentMap<K,V> extends Map<K,V> {
   ...
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions