generics

Any VBNET equivalence of C# where generic constraint keyword?

自作多情 提交于 2020-02-27 15:08:21
问题 First, I wish to write myself a generic type for operations against the underlying Active Directory. For those of you who know about AD and the System.DirectoryServices namespace, the DirectoryEntry class is the top most important along with the DirectorySearcher class. When speaking the AD language, everything is a DirectoryEntry. With that said, my application needs to manage Users, Groups and Organizational Units (OU). Each of these objects are AD entries. Then, this sounds to me a good

Is it possible to constrain a generic type defined in a jsDoc @template declaration?

此生再无相见时 提交于 2020-02-27 06:34:11
问题 We'd like to define mixin classes in regular .js files using jsDoc comments instead of .ts files. An important aspect of mixin classes is constraining the generic type parameter to a class constructor using extends . E.g., the above page has the following TypeScript: type Constructor<T> = new(...args: any[]) => T; function Tagged<T extends Constructor<{}>>(Base: T) { ... } TypeScript's jsDoc support allows for a @template T declaration, but we don't see any way to constrain T to, for example,

Return copy of case class from generic function without runtime cast

落花浮王杯 提交于 2020-02-26 18:24:32
问题 I want to get rid of a runtime cast to a generic ( asInstanceOf[A] ) without implicit conversions. This happens when I have a fairly clean data model consisting of case classes with a common trait and want to implement a generic algorithm on it. As an example the resulting algorithm should take a class of type A that is a subclass of the trait T and is supposed to return a copy of the concrete class A with some updated field. This is easy to achieve when I can simply add an abstract copy

How to pass protocol with associated type (generic protocol) as parameter in Swift?

限于喜欢 提交于 2020-02-26 09:14:42
问题 I have to pass an interface as a parameter to a function. Interface is generic a.k.a. has a associated type. I couldn't find a good way to do that. Here is my code: protocol IObserver : class { typealias DelegateT ... } class Observer: IObserver { typealias DelegateT = IGeneralEventsDelegate // IGeneralEventsDelegate is a protocol ... } func notify(observer: IObserver) { ... } // here I need a type for observer param I found that this will work: func notify<T: IObserver where T.DelegateT ==

Why does `Class<T> == Boolean.class` cause a compiler error when `T extends Comparable<? super T>`?

不羁的心 提交于 2020-02-25 02:16:19
问题 I'm using generics to abstract out Comparable data types, as illustrated in the code supplied below. This case arose from a Java Swing component, specifically from attempting to adapt table model to use generics. I have a working solution to that problem (Case 3A below). However, during the process of arriving at that solution I became confused by the behavior of the compiler when I changed the class signature from T extends Comparable<T> to T extends Comparable<? super T> . Why does the

Why does `Class<T> == Boolean.class` cause a compiler error when `T extends Comparable<? super T>`?

心不动则不痛 提交于 2020-02-25 02:15:16
问题 I'm using generics to abstract out Comparable data types, as illustrated in the code supplied below. This case arose from a Java Swing component, specifically from attempting to adapt table model to use generics. I have a working solution to that problem (Case 3A below). However, during the process of arriving at that solution I became confused by the behavior of the compiler when I changed the class signature from T extends Comparable<T> to T extends Comparable<? super T> . Why does the

C# overloading with generic constraints

孤街浪徒 提交于 2020-02-24 16:13:28
问题 Why those two methods cannot have the same name? Is it because C# compiler does not take the generic type constraints into consideration for overloading? Could it be done in future releases of C#? public static TValue GetValueOrNull<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) where TValue : class { TValue value; if (dictionary.TryGetValue(key, out value)) return value; return null; } public static TValue? GetValueOrNull<TKey, TValue>(this IDictionary<TKey, TValue>

sort HashMap in reverse? [duplicate]

左心房为你撑大大i 提交于 2020-02-24 12:09:07
问题 This question already has answers here : How to use a Java8 lambda to sort a stream in reverse order? (7 answers) Comparator.reversed() does not compile using lambda (2 answers) Closed 2 years ago . So I came across this method which is able to sort HashMaps by value. public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) { return map.entrySet() .stream() .sorted(Map.Entry.comparingByValue()) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue,

C# Dynamic Cast with Reflection and Lists

本小妞迷上赌 提交于 2020-02-24 12:01:06
问题 since yesterday i'm working on a problem and i don't get it yet... I've got a class with many Methods and decide in Runtime wich Method has to be called. Every of this Methods returns a List with Elements from my Businessobjects. My Class looks this way: public class ReflectiveClass { public List<BO1> DoSomethingWithBO1(int param){ List<BO1> list = new List<BO1>(); //.... return list; } public List<BO2> DoSomethingWithBO2(int param){ List<BO2> list = new List<BO2>(); //.... return list; }

Generify transformation of hierarchical array into a flat array

自古美人都是妖i 提交于 2020-02-24 10:22:08
问题 I'm trying to generify the transformation of a hierarchical array into a flat array. I have this kind of object which has children of the same type, which has children of the same type etc.. [{ id: "123", children: [ { id: "603", children: [ { id: "684", children: [ ... ] }, { id: "456", children: [] } ] } ] }] I found a way to flatten it and I have the information of the number of nested levels. One level deep (works): let result = myArray.flat() .concat(myArray.flatMap(comm => comm.children