Skip to content

Can't implement or override java method with type parameters. #1747

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

Closed
nicolasstucki opened this issue Nov 27, 2016 · 0 comments
Closed

Can't implement or override java method with type parameters. #1747

nicolasstucki opened this issue Nov 27, 2016 · 0 comments

Comments

@nicolasstucki
Copy link
Contributor

The following code will not compile because it can't match the types method types.

class Coll[E] extends java.util.Collection[E] {
  def toArray[T](a: Array[T]): Array[T] = ???
  ...
}


-- Error: Coll.scala -----------------------------------------------------------
2 |   def toArray[T](a: Array[T]): Array[T] = ???
  |       ^
  |overriding method toArray in trait Collection of type [T] => (x$0: Array[T])Array[T];
  | method toArray of type [T] => (a: Array[T])Array[T] has incompatible type
-- Error: Coll.scala -----------------------------------------------------------
1 |class Coll[E] extends java.util.Collection[E] {
  |      ^
  |class Coll needs to be abstract, since def toArray: [T] => (x$0: Array[T])Array[T] is not defined 
  |(Note that Array[T] does not match Array[T]: their type parameters differ)

two errors found

The same happens when trying to override the method:

class Coll[E] extends java.util.AbstractSet[E] {
  override def toArray[T](a: Array[T]): Array[T] = ???
  ...
}


-- Error: Coll.scala -----------------------------------------------------------
2 |  override def toArray[T](a: Array[T]): Array[T] = ???
  |               ^
  |overriding method toArray in class AbstractCollection of type [T] => (x$0: Array[T])Array[T];
  | method toArray of type [T] => (a: Array[T])Array[T] has incompatible type

one error found
@odersky odersky self-assigned this Dec 3, 2016
OlivierBlanvillain pushed a commit to OlivierBlanvillain/dotty that referenced this issue Dec 8, 2016
OlivierBlanvillain pushed a commit to OlivierBlanvillain/dotty that referenced this issue Dec 12, 2016
odersky added a commit to dotty-staging/dotty that referenced this issue Feb 1, 2017
If one of two method types originates from Java, the two
types might match yet have different parameter signatures,
because of the way Array[T] is handled differently in Java
and Scala. The tweak takes this pecularity into account.
odersky added a commit to dotty-staging/dotty that referenced this issue Jan 26, 2018
This also fixes scala#1747 by allowing the same encoding as `scalac` to cope
with overridden methods over unbounded generic arrays.
odersky added a commit to dotty-staging/dotty that referenced this issue Jan 27, 2018
This also fixes scala#1747 by allowing the same encoding as `scalac` to cope
with overridden methods over unbounded generic arrays.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants