scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

Cannot invoke java constructor with type parameters from scala

Open sugakandrey opened this issue 1 week ago • 3 comments

Compiler version

3.8.0-RC1 3.3.7

Minimized code

Foo.java

public class Foo<A> {
    public <E> Foo(E i) {}
}

Test.scala

object Test {
  val x1 = new Foo(1)
  val x2 = new Foo[Int](1)
  val x3 = new Foo[Int, Int](1)
}

Output

[error] -- [E007] Type Mismatch Error: Test.scala:2:8 
[error] 10 |  val x1 = new Foo(1)
[error]    |        ^
[error]    |        Found:    Foo[Object, E]
[error]    |        Required: Foo[Object, E]
[error]    |
[error]    | longer explanation available when compiling with `-explain`
[error] -- [E023] Syntax Error: Test.scala:3:15 
[error] 11 |  val x2 = new Foo[Int](1)
[error]    |               ^^^^^^^^^^^^^^^^
[error]    |               Not enough type arguments for Foo.<init>[A, E]
[error]    |               expected: [A, E]
[error]    |               actual:   [Int]
[error]    |
[error]    | longer explanation available when compiling with `-explain`
[error] -- [E023] Syntax Error: Test.scala:4:15 
[error] 12 |  val x3 = new Foo[Int, Int](1)
[error]    |               ^^^^^^^^^^^^^^^^^^^^^
[error]    |               Too many type arguments for Foo[A]
[error]    |               expected: [A]
[error]    |               actual:   [Int, Int]
[error]    |
[error]    | longer explanation available when compiling with `-explain`
[error] three errors found
[error] (Compile / compileIncremental) Compilation failed

Expectation

I expect both x1 and x2 to compile cleanly. I only included x3 as a joke, since that is what the compiler urges you to write with that Not enough type arguments error message.

sugakandrey avatar Dec 02 '25 12:12 sugakandrey