What is the Java-compatibile equivalent in Scala for <? extends Foo>

谁说我不能喝 提交于 2019-12-11 17:56:02

问题


I have a java class "JavaClass" with a method:

boolean addAll(java.util.Collection<? extends java.lang.Integer> collection) { ... }

I need to create a Scala trait MyTrait that includes this method without an implementation, something like

def addAll[A<:java.lang.Integer](coll: java.util.Collection[A]) : Boolean

so that an implementation

class MyClass extends JavaClass with MyTrait

would not be abstract. But I can't figure out any way to match the Java signature in Trait.

FWIW... I'm trying to write some high-performance Scala-collection-compatible wrappers for Trove, which avoids generics to provide high performance with primitives, but used highly consistent interfaces for each primitive type. I've found that in general, traits work well to model the type-independent demi-interfaces, but I can't figure out how to express this particular signature - even ignoring, for the moment, the type-abstraction I'm using the trait for.


回答1:


Collection<? extends java.lang.Integer> in java is equivalent to

Collection[T] forSome {type T <: java.lang.Integer}

or using placeholder syntax Collection[_ <: java.lang.Integer] in scala



来源:https://stackoverflow.com/questions/21804908/what-is-the-java-compatibile-equivalent-in-scala-for-extends-foo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!