Generic bounds with pre-generics raw types

大兔子大兔子 提交于 2019-12-11 08:04:15

问题


I've got a Collection with the element type of <K extends Comparable<K>> because it's ordered.

I'm trying to use Joda LocalTime with this collection - e.g. MyCollection<LocalTime>. Unfortunately, apparently Joda is pre-generics; LocalTime implements raw Comparable.

I'm getting the compiler error

Bound mismatch: The type LocalTime is not a valid substitute 
for the bounded parameter <K extends Comparable<K>> of the type Interval<K>

I would expect this to work with just a warning, as it's quite possible to assign a raw type to a typed variable.

Why doesn't this work, and what can I do about it?

Here's a minimal code sample:

class Holder<K extends Comparable<K>> { }
class WTF extends Holder<LocalTime> { }

回答1:


You could create a wrapper class around LocalTime which implements Comparable<K> and passes through all the method calls.



来源:https://stackoverflow.com/questions/4221431/generic-bounds-with-pre-generics-raw-types

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