generics

Why are contravariant type parameters in function parameters considered in “out” position?

纵然是瞬间 提交于 2020-02-02 11:21:49
问题 Hard for me to describe in english, but here's the issue: class Consumer<in T> { fun consume(t: T) {} } class Accepter<in T>() { // ERROR: Type parameter T is declared as 'in' but occurs in 'out' position in type Consumer<T> fun acceptWith(value: T, consumer: Consumer<T>) {} } It can be fixed like this: fun <U : T> acceptWith(value: T, consumer: Consumer<U>) {} But I don't understand the issue. It doesn't seem unsafe to allow Consumer<T> . Can someone explain this? 回答1: The argument position

Java generic method constraints - exclude type from constraint to prevent erasure problems?

∥☆過路亽.° 提交于 2020-02-02 10:14:08
问题 I am trying to overload a method based on type constraints. The code looks something like this: protected static <T extends ComponentTemplate> void addComponent(List<ComponentTemplate> factors, T component) { ... } protected static <T extends ComponentTemplate & ConditionalComponent> void addComponent(List<ComponentTemplate> factors, T conditionalComponent) { .... } ComponentTemplate is an abstract class, and ConditionalComponent is an interface. These two methods can add a component to a

Java generic method constraints - exclude type from constraint to prevent erasure problems?

这一生的挚爱 提交于 2020-02-02 10:10:11
问题 I am trying to overload a method based on type constraints. The code looks something like this: protected static <T extends ComponentTemplate> void addComponent(List<ComponentTemplate> factors, T component) { ... } protected static <T extends ComponentTemplate & ConditionalComponent> void addComponent(List<ComponentTemplate> factors, T conditionalComponent) { .... } ComponentTemplate is an abstract class, and ConditionalComponent is an interface. These two methods can add a component to a

How to use generics as a replacement for a bunch of overloaded methods working with different types?

久未见 提交于 2020-02-02 07:36:44
问题 I have a bunch of overloaded methods, all of them get an array of some type as a parameter and return a random value from that array: function GetRandomValueFromArray(Arr: array of String): String; overload; begin Result := Arr[Low(Arr) + Random(High(Arr) + 1)]; end; function GetRandomValueFromArray(Arr: array of Integer): Integer; overload; begin Result := Arr[Low(Arr) + Random(High(Arr) + 1)]; end; etc. How can I use generics to create a single method for array of any type? Something like

Using generics in Java to avoid code duplication

那年仲夏 提交于 2020-02-02 06:52:26
问题 I've defined two classes: public class GreffonTramway extends Tramway { private ServiceCollecte collecte; public GreffonTramway(int nbPlaceAssise, int nbPlaceDebout, ServiceCollecte coll){ super(nbPlaceAssise, nbPlaceDebout); collecte = coll; } ... } public class GreffonAutobus extends Autobus { private ServiceCollecte collecte; public GreffonAutobus(int nbPlaceAssise, int nbPlaceDebout, ServiceCollecte coll){ super(nbPlaceAssise, nbPlaceDebout); collecte = coll; } ... } The only difference

How to pass a type to generic method in Kotlin?

孤街浪徒 提交于 2020-02-02 04:13:09
问题 I have a generic method like below private fun <T> getSomething(): T { return "something" as T } How can I call this method with a variable T type? val types = arrayListOf<Type>(String::class.java, Boolean::class.java) types.forEach { type -> val something = getSomething<type>() // Unresolved reference: type } At runtime, I don't know what would be generic type T . I am getting the type from types and should pass it with generic getSomething method. Use case I want to call database which has

Delphi: RTTI and TObjectList<TObject>

此生再无相见时 提交于 2020-02-02 04:07:56
问题 Based on one answer to an earlier post, I'm investigating the possibility of the following design TChildClass = class(TObject) private FField1: string; FField2: string; end; TMyClass = class(TObject) private FField1: TChildClass; FField2: TObjectList<TChildClass>; end; Now, in the real world, TMyClass will have 10 different lists like this, so I would like to be able to address these lists using RTTI. However, I'm not interested in the other fields of this class, so I need to check if a

Delphi: RTTI and TObjectList<TObject>

笑着哭i 提交于 2020-02-02 04:06:47
问题 Based on one answer to an earlier post, I'm investigating the possibility of the following design TChildClass = class(TObject) private FField1: string; FField2: string; end; TMyClass = class(TObject) private FField1: TChildClass; FField2: TObjectList<TChildClass>; end; Now, in the real world, TMyClass will have 10 different lists like this, so I would like to be able to address these lists using RTTI. However, I'm not interested in the other fields of this class, so I need to check if a

Error casting a generic type to a concrete one

时光怂恿深爱的人放手 提交于 2020-02-01 10:07:31
问题 I have the following TypeScript function: add(element: T) { if (element instanceof class1) (<class1>element).owner = 100; } the problem is that I'm getting the following error: error TS2012: Cannot convert 'T' to 'class1' Any ideas? 回答1: There is no guarantee that your types are compatible, so you have to double-cast, as per the following... class class1 { constructor(public owner: number) { } } class Example<T> { add(element: T) { if (element instanceof class1) { (<class1><any>element).owner

Infer Generic Type in Class Method with Swift

孤人 提交于 2020-02-01 08:49:30
问题 Is it possible for a generic method to infer its type based on the class in which it is being executed? I use CoreData NSManagedObject models to store and retrieve local data, and have managed to make everything generic in an easy to read and usable way, except for in one place. If a user wishes to query the local database to fetch a list of objects, he would write the following line: let posts: [Post] = Post.all() This will properly return "all" Post objects in the database, but the syntax