generics

Generic class type parameter detail at runtime

允我心安 提交于 2021-01-19 22:03:33
问题 Libraries like Jackson can create objects from JSON if we provide enough information about generics. In Jackson, we can do Class<?>[] parameterTypes; JavaType type = objectMapper.getTypeFactory().constructParametricType(ObjectClass, parameterTypes); objectMapper.readValue(json, type); In java, a Generic class can be defined in many ways, like one generic class having another generic class and that can have another generic class, for simple illustration consider these three classes. public

Scala: Traversable foreach definition

☆樱花仙子☆ 提交于 2021-01-08 06:29:33
问题 I suppose foreach method in Traversable trait is defined as follows: def foreach[U](f: Elem => U) Should it be defined as foreach[U,Elem](f:Elem =>U) as there are two types, Elem and U ? 回答1: foreach[U](A => U) is a method in the trait Traversable[+A] . trait Traversable[+A] extends TraversableLike[A, ...] { ... } trait TraversableLike[+A, ...] extends ... { ... def foreach[U](f: A => U): Unit ... } https://github.com/scala/scala/blob/2.12.x/src/library/scala/collection/TraversableLike.scala

Decide if class has generic ancestor of specific generic argument type

六月ゝ 毕业季﹏ 提交于 2021-01-07 02:32:12
问题 Say I have a class CoolStorageClass , which inherits from StorageClassBase : public abstract class StorageClassBase { } public class CoolStorageClass : StorageClassBase { } Then I have a generic abstract BaseClass<T> . It is important, that T can only be of type StorageClassBase . public abstract class BaseClass<T> where T : StorageClassBase { } Then I have the implementation of the BaseClass with T as CoolStorageClass in the form of CoolClass : public class CoolClass : BaseClass

Decide if class has generic ancestor of specific generic argument type

前提是你 提交于 2021-01-07 02:31:04
问题 Say I have a class CoolStorageClass , which inherits from StorageClassBase : public abstract class StorageClassBase { } public class CoolStorageClass : StorageClassBase { } Then I have a generic abstract BaseClass<T> . It is important, that T can only be of type StorageClassBase . public abstract class BaseClass<T> where T : StorageClassBase { } Then I have the implementation of the BaseClass with T as CoolStorageClass in the form of CoolClass : public class CoolClass : BaseClass

Is it possible to have multiple type constaints on a generic type?

只愿长相守 提交于 2021-01-06 04:29:08
问题 In dart you can do class Preference<T extends int> to define a type constraint. But is there a way to define multiple constrains? I tried class Preference<T extends int, String> But an error get's thrown when I try to pass a argument of type T to a function that excepts a String saying The argument type 'T' can't be assigned to the parameter type 'String' 回答1: No, Dart type parameters can only have a single constraint. There is no workaround. 来源: https://stackoverflow.com/questions/61820181

Generic to assess if a parameter is “instanceof a generic class” if constructor is private

最后都变了- 提交于 2021-01-05 09:11:40
问题 I am trying to assert in a function if an object is instance of a class with type predicate. function assertClass<CL extends new (...args: any[]) => any>(v: any, theClass: CL, message: string): v is CL { let is = v instanceof theClass ; assert(is, message); return is; } (credit) And it works except when the constructor of the class is private class Cat{} class Dog{ private constructor(){ } static async build(){ let ret = new Dog(); await somethingElse(); return ret; } } let animal: Cat | Dog;

Why does my self-bound generic type not match the method invocation?

二次信任 提交于 2021-01-04 06:42:06
问题 I'm writing code for a command shell that looks like this: interface Context<C extends Context<C>> {} interface RecursiveContext<C extends RecursiveContext<C>> extends Context<C> { Shell<C> getShell(); default Result execute(Command cmd) { return getShell().execute(cmd, this); } } interface Shell<C extends Context<C>> { C newContext(); Result execute(Command cmd, C context); } I'm getting an error in the default method saying The method execute(Command, C) in the type Shell<C> is not

Why does my self-bound generic type not match the method invocation?

我怕爱的太早我们不能终老 提交于 2021-01-04 06:41:51
问题 I'm writing code for a command shell that looks like this: interface Context<C extends Context<C>> {} interface RecursiveContext<C extends RecursiveContext<C>> extends Context<C> { Shell<C> getShell(); default Result execute(Command cmd) { return getShell().execute(cmd, this); } } interface Shell<C extends Context<C>> { C newContext(); Result execute(Command cmd, C context); } I'm getting an error in the default method saying The method execute(Command, C) in the type Shell<C> is not

Generic type in useReducer for a returned parameter

百般思念 提交于 2021-01-03 06:34:20
问题 I am writing a custom hook to fetch some data from an API. I would like the returned data to be type-safe if possible. Can this be done with generics? type Action = { type: 'PENDING' } | { type: 'SUCCESS'; payload: any } | { type: 'FAIL' }; interface State { isLoading: boolean; isError: boolean; data: any; } const dataFetchReducer = (state: State, action: Action): State => { switch (action.type) { case 'PENDING': return { ...state, isLoading: true, }; case 'SUCCESS': { return { ...state,

How to specify anonymous object as generic parameter?

做~自己de王妃 提交于 2021-01-02 18:02:20
问题 Let's suppose I have the following task: var task = _entityManager.UseRepositoryAsync(async (repo) => { IEnumerable<Entity> found = //... Get from repository return new { Data = found.ToList() }; } What is the type of task ? Actually, it turns out to be: System.Threading.Tasks.Task<'a> , where 'a is anonymous type: { List<object> Data } How can I explicitly state this type without using var ? I have tried Task<a'> task = ... or Task<object> task = ... but can't manage it to compile. Why do I