generics

Placing different generic types into a data structure, and then handling them upon removal

元气小坏坏 提交于 2020-01-25 06:59:42
问题 I posted this question yesterday but it was marked as a duplicate of C# - Multiple generic types in one list To clarify, the answer in that problem shows how to correctly place multiple generic types into a single list. However that's not my problem. My problem is that, upon removing items from the list, I need to know what type they are in order to handle them correctly. As a starting point, I am using the answer to the above question because it does have a good solution for placing items of

Adding Click listener to Generic Adapter

落爺英雄遲暮 提交于 2020-01-25 06:47:12
问题 I have created a Generic Adapter which will be used for any type of Data and any number of items (row_layout) in RecyclerView. I have added clicklistener to it's GeneralViewHolder .Is there any flaw or issue in this adapter. And is there any callback which reduces recyclerView ? GenericAdapter.kt class GenericAdapter<T>(protected val items: List<T>, val adapterBind: OnAdapterBind<T>) : RecyclerView.Adapter<GeneralViewHolder<T>>() { override fun onCreateViewHolder(parent: ViewGroup, viewType:

Method return type contains subclass definition

会有一股神秘感。 提交于 2020-01-25 03:41:11
问题 I come up with a code that uses a syntax like this: public <A extends B> double[][] foo(C arg) { .... } I get a couple of questions by viewing it. a) The return type of foo(C arg) is <A extends B> double[][] ? What does this mean? I would understood a return type like double[][] for example, but I cannot determine what does the previous modifier (maybe?) <A extends B> does? b) Why there is a subclass declaration inside a return type? Since A is a subclass of B where do we override or add any

Interface using generics “Cannot implicitly convert type”

我的梦境 提交于 2020-01-25 03:03:06
问题 If i have a class which accepts a generic type derived from IUser, how can i avoid this error message Cannot implicitly convert type ElimCMS.Service.Users.someclass<ElimCMS.DataModel.Users.User> to ElimCMS.Service.Users.Isomeclass<ElimCMS.DataModel.Users.IUser> . An explicit conversion exists (are you missing a cast?) Example public interface Isomeclass<TUser> where TUser : class, IUser { string test(TUser user); TUser returnUser(); } public class someclass<TUser> : Isomeclass<TUser> where

Why is the following java code leads to compilation error

﹥>﹥吖頭↗ 提交于 2020-01-24 21:16:13
问题 I am currently working on making my code contain more generics. I encountered a compilation error which looks quite complicated but which I was able to reduce to an equivalent error in the following code: List<List<?>> a = new ArrayList<List<Integer>>(); Why this happens? What can I do to fix it? 回答1: Instances of a generic class with different type parameters are not related, i.e. even though String is a subtype of Object , List<String> is not a subtype of List<Object> , and even though List

TypeScript discriminated union with generics

别等时光非礼了梦想. 提交于 2020-01-24 20:50:09
问题 Is there a way to set up a discriminated union that lets you capture a specific type with each union member? I'm trying to write a type safe command handler along the lines of: interface GetUsersCommand { // returns User[] type: 'GET_USERS' } interface UpdateUserNameCommand { // returns void type: 'UPDATE_USER_NAME' userId: string name: string } type Command<Result> = GetUsersCommand | UpdateUserNameCommand class CommandExecutor { execute<Result>(command: Command<Result>): Result { switch

Given this typesafe method accepting one argument, can you generalize it to N arguments while preserving type safety?

岁酱吖の 提交于 2020-01-24 20:48:44
问题 Background: This is a follow up to two previous questions: 1, 2 Interface Extendable is generic on type T and has method extend that accepts an argument of type U and returns an argument of type V : interface Extendable<T> { extend<U, V>(x: U): V } Method extend has the following constraints: U must be assignable to T or T must be assignable to U . If U is assignable to T then V has type T . Otherwise V has type U . Interface Extendable is updated below to reflect these constraints: interface

Cyclic generic dependenices

こ雲淡風輕ζ 提交于 2020-01-24 19:35:26
问题 Let's say I have two classes. Each class has one parameter. Parameter of first class bounded to a second class and vice versa. But there is an additional requirement. This parameter must also be parametrized by class itself. This is better to be explained by example: public class Class1<T extends Class2<Class1>> { ... } public class Class2<T extends Class1<Class2>> { ... } However, this construction doesn't work. Compiler tells Type parameter Class2 is not within its bound . This is perfectly

Accessing Field Variable from Generic Object

拥有回忆 提交于 2020-01-24 16:32:39
问题 I have two classes, ClassOne and ClassTwo , that update a public field data i.e., public class ClassOne { public byte[] data = new byte[10]; // Thread that updates data } and public class ClassTwo { public byte[] data = new byte[10]; // Thread that updates data } Only one class and its associated thread is running at any given time, i.e., the app detects the source of the data at run time and uses either ClassOne or ClassTwo . I want to parse the data in a separate class called ParseMyData ,

Compilation error using Hamcrest everyItem()

可紊 提交于 2020-01-24 13:05:07
问题 I'm trying to use hasKey() on everyItem() in Hamcrest but I am getting the following compilation error: error: no suitable method found for assertThat(List<Map<String,Object>>,Matcher<Iterable<Map<? extends String,?>>>) assertThat(data, everyItem(hasKey("index"))); ^ method Assert.<T#1>assertThat(T#1,Matcher<? super T#1>) is not applicable (actual argument Matcher<Iterable<Map<? extends String,?>>> cannot be converted to Matcher<? super List<Map<String,Object>>> by method invocation