Skip to content
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

Scala tries to pass primitive arrays to Java methods expecting polymorphic array types #750

Closed
scabug opened this issue Apr 15, 2008 · 6 comments
Assignees
Labels

Comments

@scabug
Copy link

@scabug scabug commented Apr 15, 2008

Spotted by Amit Dev

Java:

public class AO {
    public static <T> void f(T[] a) {
        System.out.println(ar0);
    }
}

Scala:

object Main {
    def main(argv : Array[String]) : Unit = {
        val a = Array(1, 2, 3)
        AO.f(a)
    }
}

This tries to cast a to Object[], which will always fail. It should give a compile-time error, since Java doesn't support polymorphic use of primitive arrays.

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Apr 15, 2008

Imported From: https://issues.scala-lang.org/browse/SI-750?orig=1
Reporter: Jamie Webb (jwebb)

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Aug 7, 2008

@odersky said:
This will depend on the question whether or not to restrict Java generics to reference types. Postponed until we decide that question.

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Jan 14, 2009

@odersky said:
Milestone postponed deleted

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Jul 3, 2010

@paulp said:
This is maybe better, maybe worse now: it compiles from all source, but not against the bytecode.

[paulp@leaf 750]$$ scalac AO.java a.scala 
[paulp@leaf 750]$$ scala Main
java.lang.ClassCastException: [I cannot be cast to [Ljava.lang.Object;
	at Main$$.main(a.scala:4)
	at Main.main(a.scala)

But:

[paulp@leaf 750]$$ javac AO.java 
[paulp@leaf 750]$$ scalac a.scala 
a.scala:4: error: type mismatch;
 found   : Array[Int]
 required: Array[? with java.lang.Object]
        AO.f(a)
             ^
one error found
@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Jun 24, 2011

@paulp said:
Consolidating with closely related #4216.

@scabug scabug closed this Jun 24, 2011
@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented May 6, 2012

@retronym said:
It's working in both joint and separate compilation now. Test cases, and a few more comments: scala/scala#479

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.