bounded-wildcard

Java F-Bound types with generics

怎甘沉沦 提交于 2019-12-08 04:18:46
问题 Is there any way to express f-bound types in java where at the call site, a generic response is returned? interface Functor<T extends Functor<T>> public <B> T<B> map(Function<A, B> fn); // won't compile because types don't match I can use f-bound types if the type never changes, but in the case of map, I need a new type. Is there a way to express this in java? What I am really looking for is any way that I can get something like higher kinds even though I know javac doesn't support higher

Is it possible to write a single method that accepts a generic parameter of varying abstraction?

社会主义新天地 提交于 2019-12-07 08:20:37
问题 As a followup to this question, is it possible to write a single method that adds a Dog to a suitable room? (In this example, it would accept either an Animal room or a Dog room.) Or am I forced to write two distinct methods as below? (I can't even rely on overloading because of type erasure). public class Rooms { interface Animal {} class Dog implements Animal {} class Room<T> { void add(T t) {} } void addDogToAnimalRoom(Room<Animal> room) { room.add(new Dog()); } void addDogToDogRoom(Room

Use generic to store common supertype in Java

﹥>﹥吖頭↗ 提交于 2019-12-06 18:40:26
问题 Suppose I have a method "mix" that takes two Lists of possibly different types T and S and returns a single List containing the elements of both. For type-safety, I'd like to specify that the returned List is of a type R, where R is a supertype common to both T and S. For example: List<Number> foo = mix( Arrays.asList<Integer>(1, 2, 3), Arrays.asList<Double>(1.0, 2.0, 3.0) ); To specify this, I could declare the method as static <R, T extends R, S extends R> List<R> mix(List<T> ts, List<S> ss

Raw types, unbounded wilcard and bounded wildcard

一笑奈何 提交于 2019-12-06 09:19:00
I have a quick question as below: Here's a simple examples about this whole issues: List a = new ArrayList(); List <?> b; List <? extends Object> c; According to Java SCJP by khalid mughal (a very good book!): a = b; // ok. Widening conversion. b = a; // ok too. No unchecked warning. b = c; // ok c = b; // ok c=a; // ok but now will issue a unchecked warning. // clause 1 I do understand that any raw types (example a) when assigned to any bounded wilcard references, a unchecked warning is issues (since the content in that raw type a could be anything). My questions is since c is the highest

A bad interaction between self-referential types and bounded wildcards

两盒软妹~` 提交于 2019-12-06 00:44:13
This case seems to be another one where Eclipse's Java compiler crushes javac. The only question for me is whether it's a bug in JLS or javac. interface EndoFunctor< C, FC extends EndoFunctor< C, FC > > { /*...*/ } interface Algebra< C, FC extends EndoFunctor< ? extends C, FC > > { /*...*/ } The second line compiles in Eclipse, but fails to compile in javac with the message that "type parameter FC is not within its bound". FC is declared to extend EndoFunctor< ? extends C, FC >, and the bound on FC is that it extend EndoFunctor< D, FC > for the inferred D, which in this case is ? extends C. I

Is it possible to write a single method that accepts a generic parameter of varying abstraction?

不想你离开。 提交于 2019-12-05 17:48:20
As a followup to this question , is it possible to write a single method that adds a Dog to a suitable room? (In this example, it would accept either an Animal room or a Dog room.) Or am I forced to write two distinct methods as below? (I can't even rely on overloading because of type erasure). public class Rooms { interface Animal {} class Dog implements Animal {} class Room<T> { void add(T t) {} } void addDogToAnimalRoom(Room<Animal> room) { room.add(new Dog()); } void addDogToDogRoom(Room<Dog> room) { room.add(new Dog()); } } Louis Wasserman You're using Room as a consumer, since it's

Lower bounded wildcard not checked against upper bounded type parameter

南楼画角 提交于 2019-12-05 16:10:39
问题 I wonder why does this piece of code compile successfully? Source code: abstract class A<K extends Number> { public abstract <M> A<? super M> useMe(A<? super M> k); } Compiled successfully How does it work and why does this compile? M is any type, so why it can be used?. Should it be: <M extends Number> ? This will not compile: abstract class A<K extends Number> { public abstract <M> A<? super M> useMe(A<M> k); } Error message: type argument M is not within bounds of type variable K where M,

Use generic to store common supertype in Java

半城伤御伤魂 提交于 2019-12-05 01:20:02
Suppose I have a method "mix" that takes two Lists of possibly different types T and S and returns a single List containing the elements of both. For type-safety, I'd like to specify that the returned List is of a type R, where R is a supertype common to both T and S. For example: List<Number> foo = mix( Arrays.asList<Integer>(1, 2, 3), Arrays.asList<Double>(1.0, 2.0, 3.0) ); To specify this, I could declare the method as static <R, T extends R, S extends R> List<R> mix(List<T> ts, List<S> ss) But what if I want to make mix an instance method instead of static, on the class List2<T> ? <R, T

.NET equivalent for Java bounded wildcard (IInterf<?>)?

流过昼夜 提交于 2019-12-04 12:17:18
问题 I'm stuck trying to translate some Java code that uses (bounded) wildcard generics to C#. My problem is, Java seems to allow a generic type to be both covariant and contravariant when used with a wildcard. [This is a spin-off from a previous question dealing with a simpler case of bounded-wildcards] Java - works: class Impl { } interface IGeneric1<T extends Impl> { void method1(IGeneric2<?> val); T method1WithParam(T val); } interface IGeneric2<T extends Impl> { void method2(IGeneric1<?> val)

difference between creation unbounded and bounded wild card type array?

六月ゝ 毕业季﹏ 提交于 2019-12-04 03:01:04
Why is this code valid ArrayList<?>[] arr = new ArrayList<?>[2]; but the following two are not? ArrayList<? extends Object>[] arr = new ArrayList<? extends Object>[2]; ArrayList<? super Object>[] arr = new ArrayList<? super Object>[2]; The two last rows generate the compile error; error: generic array creation. Please clarify difference. update On the other hand ArrayList<?>[] arr = new ArrayList<?>[2]; compiles good but ArrayList<?> arr = new ArrayList<?>(); not. There are a few issues going on here, lets look at each in turn: A type bound (ie extends Object ) can only be declared when