-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CollectionConverters conversion between java.lang.Long and scala.Long #16349
Comments
Implicit conversions are only applied at the top-level. If there is an implicit conversion |
Thanks for the reply! def maps(): Map[String, Long] = L.maps().asScala.toMap.collect { case (k, v) if v > 0L => (k, v)} does compile with Scala 2.13.10, but fails with the above error in Scala 3.x. I created this issue to understand if 3.x is more correct, of if 2.13 is wrong to apply the conversion. |
@DieBauer I get the same error with Scala 2.13.10, can you check again?
|
Correct! That was a mistake from my side to include that statement. The fact is that the second approach, with import test._
class Call {
def x(): Long = L.y()
import scala.jdk.CollectionConverters._
def maps(): Map[String, Long] = L.maps().asScala.toMap.collect { case (k, v) if v > 0L => (k, v)}
} Could you see that this does compile with 2.13.10 and fails with 3.2.1? |
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 Scala 3 output package <empty> {
class B() extends Object() {
import scala.jdk.CollectionConverters.*
def maps(): Map[String, Long] =
jdk.CollectionConverters.MapHasAsScala[String, Long](L.maps()).asScala.
toMap
[String, Long](<:<.refl[(String, Long)]).collect[String, Long](
{
def $anonfun(x$1: (String, Long)): (String, Long) =
x$1:(x$1 : (String, Long)) @unchecked match
{
case Tuple2.unapply[String, Long](k @ _, v @ _) if
Long2long(v).>(0L)
=>
Tuple2.apply[String, Long](k, v)
}
closure($anonfun:PartialFunction[(String, Long), (String, Long)])
}
)
}
} Scala 2 output package <empty> {
class B extends scala.AnyRef {
def <init>(): B = {
B.super.<init>();
()
};
import scala.jdk.CollectionConverters._;
def maps(): Map[String,Long] = scala.jdk.CollectionConverters.MapHasAsScala[String, Long](L.maps()).asScala.toMap[String, Long](scala.this.<:<.refl[(String, Long)]).collect[String, Long](({
@SerialVersionUID(value = 0) final <synthetic> class $anonfun extends scala.runtime.AbstractPartialFunction[(String, Long),(String, Long)] with java.io.Serializable {
def <init>(): <$anon: ((String, Long)) => (String, Long)> = {
$anonfun.super.<init>();
()
};
final override def applyOrElse[A1 <: (String, Long), B1 >: (String, Long)](x1: A1, default: A1 => B1): B1 = ((x1.asInstanceOf[(String, Long)]: (String, Long)): (String, Long) @unchecked) match {
case (_1: String, _2: Long): (String, Long)((k @ _), (v @ _)) if scala.Predef.Long2long(v).>(0L) => scala.Tuple2.apply[String, Long](k, scala.Predef.Long2long(v))
case (defaultCase$ @ _) => default.apply(x1)
};
final def isDefinedAt(x1: (String, Long)): Boolean = ((x1.asInstanceOf[(String, Long)]: (String, Long)): (String, Long) @unchecked) match {
case (_1: String, _2: Long): (String, Long)((k @ _), (v @ _)) if scala.Predef.Long2long(v).>(0L) => true
case (defaultCase$ @ _) => false
}
};
new $anonfun()
}: PartialFunction[(String, Long),(String, Long)]))
}
} |
Compiler version
Scala compiler version 3.2.1 -- Copyright 2002-2022, LAMP/EPFL
Minimized code
create java file in
./test/L.java
create following scala file in
./Call.scala
Run the following:
Output
Expectation
Scala compiler can convert java.lang.Long to scala.Long, just as the case with the single statement in the example file.
The text was updated successfully, but these errors were encountered: