generics

Assigning arrow function to generic function type with TypeScript

限于喜欢 提交于 2020-01-11 11:28:23
问题 I've done some digging on similar questions but I cannot find a solution that works. I've got some types of generic functions, but I cannot seem to implement them correctly. In short, I have this: /** Takes three values of the same type and collapses them into one */ declare type MergeFunction = <T>(def: T, file?: T, arg?: T) => T /** The implementation I'm having trouble with. Merge three arrays of the same type. */ const arrayMerge: MergeFunction = <T>(def: T[], file: T[] = [], arg: T[] = [

Given a constrained generic method can I call a non-generic method passing the actual type of the generic parameter

99封情书 提交于 2020-01-11 10:24:28
问题 Awkward title I know, this is best explained in code. Given a set of classes: public abstract class MyBaseType { public string Message { get; set; } } public class MySuperType : MyBaseType { public string AdditionalInfo { get; set; } } public class MyOtherSuperType : MyBaseType { public DateTime Started { get; set; } public DateTime Finished { get; set; } } Is there a way to write a generic method that calls a non-generic method passing the generic type whilst interpreting the passed type as

What's the best way to instantiate a generic from its name?

落爺英雄遲暮 提交于 2020-01-11 10:11:15
问题 Assuming I have only the class name of a generic as a string in the form of "MyCustomGenericCollection(of MyCustomObjectClass)" and don't know the assembly it comes from, what is the easiest way to create an instance of that object? If it helps, I know that the class implements IMyCustomInterface and is from an assembly loaded into the current AppDomain. Markus Olsson gave an excellent example here, but I don't see how to apply it to generics. 回答1: Once you parse it up, use Type.GetType

What's the best way to instantiate a generic from its name?

不想你离开。 提交于 2020-01-11 10:11:07
问题 Assuming I have only the class name of a generic as a string in the form of "MyCustomGenericCollection(of MyCustomObjectClass)" and don't know the assembly it comes from, what is the easiest way to create an instance of that object? If it helps, I know that the class implements IMyCustomInterface and is from an assembly loaded into the current AppDomain. Markus Olsson gave an excellent example here, but I don't see how to apply it to generics. 回答1: Once you parse it up, use Type.GetType

create List of Type using Type variable at runtime [duplicate]

徘徊边缘 提交于 2020-01-11 09:56:46
问题 This question already has answers here : Is it impossible to use Generics dynamically? [duplicate] (2 answers) Closed 6 years ago . I'm trying to simplify my methods in creating several variables in a function which is repeated for different types and different actions, eventually I will migrate to a single function that can handle multiple types but I'm doing it in stages. I would like to be able to make a list of objects in their types instead of List without having to copy/paste the object

Erasure of method is the same as another method in type

瘦欲@ 提交于 2020-01-11 09:35:11
问题 Following are two methods in a class class Developer and Student are different and not share any parent. Both following method has same code and method called on both object have same name ex. getStartDate. class hierarchyValidator { private LocalDate[][] getArrayOfTimespan(List<Developer> developers) { //here i have to call some method on developer from list LocalDate startDate = developer.getStartDate(); .... } private LocalDate[][] getArrayOfTimespan(List<Student> students) { //here i have

Using System.Type to call a generic method

删除回忆录丶 提交于 2020-01-11 08:48:18
问题 I am using C#/.NET 4.0 and a Protocol Buffers library (protobuf-net) which provides the following functionality. public static class Serializer { public static void Serialize<T>(Stream destination, T instance); public static void Serialize<T>(SerializationInfo info, T instance); public static void Serialize<T>(XmlWriter writer, T instance); public static void Serialize<T>(SerializationInfo info, StreamingContext context, T instance); public static T Deserialize<T>(Stream source); } I need to

Jackson module signature prevents addings serializers for self-referencing generic types

♀尐吖头ヾ 提交于 2020-01-11 08:22:40
问题 I want to add a custom serializer and deserializer for JSR 363 javax.measure.Quantity<Q extends Quantity<Q>> , which basically encapsulates a "value" and a "unit". Creating the serializer ( extends JsonSerializer<Quantity<?>> ) and the deserializer ( extends StdDeserializer<Quantity<?>> ) is easy. But it's not easy to register them. For deserializers, it's OK; look at the signature: SimpleModule.addDeserializer(Class<T> type, JsonDeserializer<? extends T> deser) Notice that the deserializer

How is the generic type getting inferred here?

不想你离开。 提交于 2020-01-11 08:21:26
问题 public static void main(String[] args) { Map<String, Map<Long, List<String>>> map = getHashMap(); } static <K,V> Map<K,V> getHashMap() { return new HashMap<K, V>(); } I saw a similar code in google guava (as factory methods) for making instances of Hashmap without mentioning the generic types.I don't understand how the generic is getting inferred by the above program.I mean how can the function getHashMap understand the type of map since i'm not passing any type information to the function.

Generic base class wraps nested generic class to reduce type argument specification: Is there a name for this pattern?

青春壹個敷衍的年華 提交于 2020-01-11 07:27:06
问题 Ok question title is far from being self-explanatory. I see myself doing this often: From this answer: public static class Equality<T> { public static IEqualityComparer<T> CreateComparer<K>(Func<T, K> keySelector) { return new KeyEqualityComparer<K>(keySelector); } class KeyEqualityComparer<K> : IEqualityComparer<T> { readonly Func<T, K> keySelector; public KeyEqualityComparer(Func<T, K> keySelector) { this.keySelector = keySelector; } public bool Equals(T x, T y) { ---- } public int