Hi there,
I mostly work with Scala and don't know too much about the internal machinery of Java. There is something very confusing to me. Since Java generics don't support type variations like Scala, why do you have things like ``` public class TumblingEventTimeWindows extends WindowAssigner<Object, TimeWindow> ``` instead of ``` public class TumblingEventTimeWindows<T> extends WindowAssigner<T, TimeWindow> ``` ? But here it seems the compiler can infer the type `WindowAssigner<Tuple2<String, Integer>, TimeWindow>` instead of `WindowAssigner<Object, TimeWindow>`. Is that so? When I try to do the same in Scala, the compiler gives an error that `Object` is invariant. It would be great if someone can give me a hint. Kind regards, Zhenhao |
Maybe this SO post can shed some light on the matter:
https://stackoverflow.com/questions/4343202/difference-between-super-t-and-extends-t-in-java
Here's also a small example you can
play around with:
public static void main(String[] args) { final Container<Integer> container = new Container<>(new ArrayList<Object>()); } private static class Container<T> { private final List<? super T> collection; private Container(List<? super T> collection) { this.collection = collection; } public void addT(T item) { collection.add(item); } // This doesn't work //public void addObject(Object item) { // collection.add(item); //} // This doesn't work //public T getT(int i) { // return collection.get(i); //} public Object getObject(int i) { return collection.get(i); } } On 5/10/2021 4:21 PM, Zhenhao Li wrote:
|
Free forum by Nabble | Edit this page |