I'm writing a Scala class that implements (wraps) a java.util.List, i.e.:
class MyList(backingList: java.util.List) extends java.util.List
The latter has a method toArray with a Java signature like this:
<T> T[] toArray(T[] a)
Naively, I wrote this as:
def toArray[T](a: Array[T]) = backingList toArray a
but the compiler complains that the call to toArray on backingList expects an Array[? with java.lang.Object]
.
I think I've tried every possible variation over things like Array[_ >: T with Object]
(which the compiler kindly suggests), but no luck. Any suggestions?
This also works:
def toArray[T](a: Array[T with Object]) = backingList.toArray[T](a)
Array[_ >: T with Object]
is syntactic sugar for Array[X forSome {type X <: Object}]
, meaning an Array of objects whose type is a subtype of Object, but what you need is an Array of objects of the same type X, which is a subtype of Object. So in short, it is just a scoping issue.
def toarray(a: Array[X] forSome {type X <: Object}) = backingList toArray a
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With