generics

Comparable VS <? extends Comparable>

北城以北 提交于 2021-02-13 12:15:07
问题 regarding the following code: public class Test <T extends Comparable>{ public static void main(String[] args){ List<String> lst = Array.asList("abc","def"); System.out.println(func(lst)); } public static boolean func(List<**here**> lst){ return lst.get(0).compareTo(lst.get(1)) == 0; } } why writing " ? extends Comparable" here would compile , and writing "Comparable" would not compile? thanks in advance. 回答1: This happens because generics are invariant . Even if String is a Comparable ,

Comparable VS <? extends Comparable>

梦想的初衷 提交于 2021-02-13 12:14:53
问题 regarding the following code: public class Test <T extends Comparable>{ public static void main(String[] args){ List<String> lst = Array.asList("abc","def"); System.out.println(func(lst)); } public static boolean func(List<**here**> lst){ return lst.get(0).compareTo(lst.get(1)) == 0; } } why writing " ? extends Comparable" here would compile , and writing "Comparable" would not compile? thanks in advance. 回答1: This happens because generics are invariant . Even if String is a Comparable ,

How to read JSON in list of generic

会有一股神秘感。 提交于 2021-02-11 17:51:26
问题 I have a helper class calling a REST web service. This helper class receives the information from the web service within a class defined as a generic "R". I can easily use GSON to read my JSON in a POJO, but I can't figure out how to give it a List and read it properly. So far I have tried to extract the type of R in a variable and pass it to the List without success. I have tried using Jackson with its TypeFactory, also without success, it won't take R as its parameter. What I want to do is

How to read JSON in list of generic

北城以北 提交于 2021-02-11 17:51:07
问题 I have a helper class calling a REST web service. This helper class receives the information from the web service within a class defined as a generic "R". I can easily use GSON to read my JSON in a POJO, but I can't figure out how to give it a List and read it properly. So far I have tried to extract the type of R in a variable and pass it to the List without success. I have tried using Jackson with its TypeFactory, also without success, it won't take R as its parameter. What I want to do is

How can you create a generic struct for Numeric numbers that can calculate a percentage?

ε祈祈猫儿з 提交于 2021-02-11 16:59:22
问题 This doesn't compile because Initializer 'init(_:)' requires that 'Number' conform to 'BinaryInteger' struct Percentage<Number: Numeric> { let value: Number let total: Number var percentage: Double { Double(value) / Double(total) } } Does anyone have a nice solution? To give some context to the problem from real life: I'm coding a SwiftUI app, that has a CircularProgress-view. I would like to use the same CircularProgress-view with different number types and to be able to show the current

C# How to use GetMethod to find a specific overload with a generic out parameter?

梦想的初衷 提交于 2021-02-11 13:48:47
问题 Everything is in the title, but let me put some code. Let's say we have a class with 2 methods with the same name: class MyClass { /// <summary> /// The first version /// </summary> public TItem Foo(long param1) { ... } /// <summary> /// Overload with a generic parameters /// </summary> public bool Foo<TItem>(out TItem item, long param1) { ... } } An we need to get the MethodInfo for the second 'Foo' method which has a generic out parameter: public class AnotherClass { public MethodInfo

Using Self as generic parameter

大兔子大兔子 提交于 2021-02-11 13:41:37
问题 I have simple generic class: class MyClass<T> { let closure: (T -> Void) init(closure: T -> Void) { self.closure = closure } } I would like to write an extension for UIView which would apply closure to any subclass of UIView : extension UIView { func apply(c: MyClass<Self>) -> Self { c.closure(self) return self } } But it gives me an error: 'Self' is only available in a protocol or as the result of a method in a class . Is there any solution to fix this code? 回答1: You can achieve this by

Java generics - bridge method

让人想犯罪 __ 提交于 2021-02-11 12:35:48
问题 When comes to bridge method, i do know that java compiler will add them if there's a need so that overriding can be done properly by the subclass (after reading SCJP by mughal and angelikalanger website). But this is a bit confusing as per below: Before erasure: class x <T> { void set(T t){} } class y <E> extends x { void set(E e) {} // name clash here } class z<E> extends x { void set(Object y) {} // no name clash here } class z1<E> extends x<T> { void set(Object y) {} // name clash here }

Why are arrays covariant but generics are invariant?

孤人 提交于 2021-02-11 12:04:35
问题 From Effective Java by Joshua Bloch, Arrays differ from generic type in two important ways. First arrays are covariant. Generics are invariant. Covariant simply means if X is subtype of Y then X[] will also be sub type of Y[]. Arrays are covariant As string is subtype of Object So String[] is subtype of Object[] Invariant simply means irrespective of X being subtype of Y or not , List<X> will not be subType of List<Y>. My question is why the decision to make arrays covariant in Java? There

Why are arrays covariant but generics are invariant?

老子叫甜甜 提交于 2021-02-11 12:02:01
问题 From Effective Java by Joshua Bloch, Arrays differ from generic type in two important ways. First arrays are covariant. Generics are invariant. Covariant simply means if X is subtype of Y then X[] will also be sub type of Y[]. Arrays are covariant As string is subtype of Object So String[] is subtype of Object[] Invariant simply means irrespective of X being subtype of Y or not , List<X> will not be subType of List<Y>. My question is why the decision to make arrays covariant in Java? There