Dotty: Interaction with Java's raw generic types is not possible

Created on 23 Nov 2017  路  6Comments  路  Source: lampepfl/dotty

See for instance Javafx's method https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TreeTableView.html#setColumnResizePolicy-javafx.util.Callback- it takes a Callback<TreeTableView.ResizeFeatures,Boolean> but do notice that ResizeFeatures defines one generic parameter, the method defines a callback that takes it raw though. In scalac, you would pass a javafx.util.Callback[javafx.scene.control.TreeTableView.ResizeFeatures[_], Boolean] and it would typecheck, but with dottyc it doesn't work

java advanced bug low

Most helpful comment

Yes, for now we've decided to not try to make the dotty Java source parser work with raw types since it's usually pretty easy to fix Java code in your own project to not require raw types. We may change this in the future but it's low priority.

All 6 comments

Reproduced on master with:

import javafx.scene.control.TreeTableView

class Test {
  def test[T](
    table: TreeTableView[T],
    callback: javafx.util.Callback[TreeTableView.ResizeFeatures[_], java.lang.Boolean],
  ): Unit = {
    table.setColumnResizePolicy(callback)
  }
}
-- [E007] Type Mismatch Error: tests/allan/Test.scala:8:32 ---------------------
8 |    table.setColumnResizePolicy(callback)
  |                                ^^^^^^^^
  |found:    javafx.util.Callback[javafx.scene.control.TreeTableView.ResizeFeatures[_], 
  |  Boolean
  |](callback)
  |required: javafx.util.Callback[javafx.scene.control.TreeTableView.ResizeFeatures, Boolean]

Note that we cannot import stuff from javafx in our testsuite: it is not part of the standard builds of openjdk and can be quite cumbersome to manually install.

@smarter Here you have an example java class

public class TryMe {
  public static <R> void ifYouCan(scala.Function1<java.util.List, R> f) {
  }
}

which when you try to call it from scala you get
```scala
object RawGenericsTest {
TryMe.ifYouCan(list => println(list))
}
[error] -- [E055] Syntax Error: RawGenericsTest.scala:2:17
error] 2 | TryMe.ifYouCan(list => println(list))
[error] | ^
[error] | missing type parameter for java.util.List


or
```scala
object RawGenericsTest {
  TryMe.ifYouCan((list: java.util.List[_]) => println(list))
}
[error] -- [E007] Type Mismatch Error: RawGenericsTest.scala:2:94 
[error] 2 |  TryMe.ifYouCan((list: java.util.List[_]) => println(list))
[error]   |                                                           ^
[error]   |                                       found:    java.util.List[_] => Unit
[error]   |                                       required: java.util.List => Unit
[error]   |                                       
[error] one error found

Thanks for the simplified testcase!

Also worth noting, that if you happen to try and compile the java source in mixed mode with dotty (by placing the file under src/main/java), then when dotty parses the java file for the signatures you get this

[error] -- Error: TryMe.java:3:72 ------
[error] 3 |  public static <R> void ifYouCan(java.util.function.Function<java.util.List, R> f) {
[error]   |                                                              ^^^^^^^^^^^^^^
[error]   |          Type argument java.util.List has not the same kind as its bound 

Yes, for now we've decided to not try to make the dotty Java source parser work with raw types since it's usually pretty easy to fix Java code in your own project to not require raw types. We may change this in the future but it's low priority.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

odersky picture odersky  路  94Comments

LPTK picture LPTK  路  35Comments

japgolly picture japgolly  路  55Comments

odersky picture odersky  路  126Comments

odersky picture odersky  路  71Comments