CollectionConverters conversion between java.lang.Long and scala.Long
See original GitHub issueCompiler version
Scala compiler version 3.2.1 – Copyright 2002-2022, LAMP/EPFL
Minimized code
create java file in ./test/L.java
package test;
import java.util.Map;
public class L {
public static Long y() {
return 123L;
}
public static Map<String, Long> maps() {
return Map.of("A", 1L);
}
}
create following scala file in ./Call.scala
import test._
class Call {
def x(): Long = L.y()
import scala.jdk.CollectionConverters._
// def mmap(): Map[String, Long] = L.maps().asScala.toMap
def maps(): Map[String, Long] = L.maps().asScala.toMap.collect { case (k, v) if v > 0L => (k, v)}
}
Run the following:
javac -d classes test/L.java
scalac -classpath ./classes Call.scala
Output
-- [E007] Type Mismatch Error: Call.scala:9:65 ---------------------------------
9 | def maps(): Map[String, Long] = L.maps().asScala.toMap.collect { case (k, v) if v > 0L => (k, v)}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| Found: PartialFunction[(String, Long), (String, Long)]
| Required: PartialFunction[(String, Long), (String, Long²)]
|
| where: Long is a class in package java.lang
| Long² is a class in package scala
|
| longer explanation available when compiling with `-explain`
2 errors found
Expectation
Scala compiler can convert java.lang.Long to scala.Long, just as the case with the single statement in the example file.
Issue Analytics
- State:
- Created 10 months ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Conversions Between Java and Scala Collections
It is quite easy to do this, because Scala offers implicit conversions between all the major collection types in the JavaConverters object.
Read more >Implicit conversion between Scala.Long and Java.lang.Long ...
Scala has implicit conversions between scala.Long and java.lang.Long , but they won't convert collections of those types.
Read more >Converting Java Collections to Scala Collections - Baeldung
A quick and practical guide to convenient Java/Scala collections conversions.
Read more >CollectionConverters - Scala 3 - EPFL
This object provides extension methods that convert between Scala and Java collections. When writing Java code, use the explicit conversion methods defined ...
Read more >How to go to and from Java collections in Scala
CollectionConverters object to convert from Scala collections classes to Java collections classes. While those classes/objects have changed, the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ah, thanks. Yes, that example compiles with 2.13.10, and compilation fails with 3.2.1.
I diffed the output after the typer phase for both scala 2 and 3, and in scala 2 the value type (java
Long
) gets an implicit conversion appliedscala.Tuple2.apply[String, Long](k, scala.Predef.Long2long(v))
. While in Scala 3 that doesn’t happenTuple2.apply[String, Long](k, v)
, even though the guard on the value0L
does get the conversion applied.Scala 3 output
Scala 2 output