generics

Overloading a method that takes a generic list with different types as a parameter

梦想的初衷 提交于 2020-01-16 20:55:42
问题 How can I overload a method that takes a Generic List with different types as a parameter? For example: I have a two methods like so: private static List<allocations> GetAllocationList(List<PAllocation> allocations) { ... } private static List<allocations> GetAllocationList(List<NPAllocation> allocations) { ... } Is there a way I can combine these 2 methods into one? 回答1: Sure can... using generics! private static List<allocations> GetAllocationList<T>(List<T> allocations) where T :

In Subsonic 2.1 how do I make this generic call take one generic parameter?

不问归期 提交于 2020-01-16 19:19:12
问题 Using Subsonic 2.1 I want to make my method call to results look like this: results(searchCriteria) right now I have to pass the CollectionType as well as the type. Animal searchCriteria = GetSearchCritera(); AnimalCollection results = results<Animal, AnimalCollection>(searchCriteria); // I want the call to be results(searchCriteria); Here is the results method that I want to just take Y public static T results<Y, T>(Y searchCriteria) where Y: ReadOnlyRecord<Y>, new() where T: ReadOnlyList<Y,

In Subsonic 2.1 how do I make this generic call take one generic parameter?

我怕爱的太早我们不能终老 提交于 2020-01-16 19:17:28
问题 Using Subsonic 2.1 I want to make my method call to results look like this: results(searchCriteria) right now I have to pass the CollectionType as well as the type. Animal searchCriteria = GetSearchCritera(); AnimalCollection results = results<Animal, AnimalCollection>(searchCriteria); // I want the call to be results(searchCriteria); Here is the results method that I want to just take Y public static T results<Y, T>(Y searchCriteria) where Y: ReadOnlyRecord<Y>, new() where T: ReadOnlyList<Y,

using static property of an abstract class inside a generic class that works on subclasses of the abstract class

假如想象 提交于 2020-01-16 18:17:23
问题 I have a question regarding the following code: abstract class a { public static string x; } class b<c> where c : a { public void f() { c.x=10; } } This code does not compile. I get an error at the statement c.x=10; . The problem makes it look as if the condition where c:a does not have any effect at all.Can someone please explain why this is an error? Is it not true that x is shared as a static member by all children of a? And is there a way to circumvent this problem? What I am trying to

Generic mapping of types in typescript

▼魔方 西西 提交于 2020-01-16 18:15:11
问题 lets imagine type T: type T = { prop1: (s: S) => T1, prop2: (s: S) => T2, prop3: (s: S) => T3, } and now lets imagine type W type W = (s: S) => { prop1: T1, prop2: T2, prop3: T3, } its easy to write a function that maps T to W by hand, is it possible to write generic type sefe version of it in typescript? function x(t: T): W { return funtion(s: S) { prop1: t.prop1(s), prop2: t.prop2(s) prop3: t.prop3(s) } } what kind of feature language is missing to facilitate this, something like higher

Struct members who are traits that use associated types

北慕城南 提交于 2020-01-16 16:31:28
问题 I have a follow up question to this question: Expose a HashMap in a generic way that disregards the HashMap value Suppose I want to use HashMapContainer (the same one that was defined in the previous question's first answer) as a member in another struct (let's call it MyDB ) and in MyDB constructor I want to decide whether to construct this member as HashMapContainerImpl1 or HashMapContainerImpl2 . I don't want to define MyDB as a template (e.g MyDB<T> ) because MyDB users don't care about

Struct members who are traits that use associated types

你说的曾经没有我的故事 提交于 2020-01-16 16:31:12
问题 I have a follow up question to this question: Expose a HashMap in a generic way that disregards the HashMap value Suppose I want to use HashMapContainer (the same one that was defined in the previous question's first answer) as a member in another struct (let's call it MyDB ) and in MyDB constructor I want to decide whether to construct this member as HashMapContainerImpl1 or HashMapContainerImpl2 . I don't want to define MyDB as a template (e.g MyDB<T> ) because MyDB users don't care about

Function pointers in Rust using constrained generics

荒凉一梦 提交于 2020-01-16 09:34:22
问题 I'm trying to create a struct that looks like this: struct MediaLibrary<B> where B: Ord, { root_dir: PathBuf, item_meta_fn: String, self_meta_fn: String, media_item_filter: fn(&Path) -> bool, media_item_sort_key: fn(&Path) -> B, } The last two fields are meant to be used as a predicate to test if a given path is a valid media file and to sort a vector of paths (using sort_by_key ), respectively. However, as it is right now, the design is inflexible: both functions are fixed to accept only

Function pointers in Rust using constrained generics

孤人 提交于 2020-01-16 09:32:51
问题 I'm trying to create a struct that looks like this: struct MediaLibrary<B> where B: Ord, { root_dir: PathBuf, item_meta_fn: String, self_meta_fn: String, media_item_filter: fn(&Path) -> bool, media_item_sort_key: fn(&Path) -> B, } The last two fields are meant to be used as a predicate to test if a given path is a valid media file and to sort a vector of paths (using sort_by_key ), respectively. However, as it is right now, the design is inflexible: both functions are fixed to accept only

Constraining generic types by extrinsic functionality

ε祈祈猫儿з 提交于 2020-01-16 09:29:15
问题 Background: I am working with an organization that has an ever-growing collection of data types that they need to extract data from. I have no ability to change these data types. Some are machine-generated from XML files provided by other organizations; some are controlled by intransigent internal dev teams; and some are so old that no one is willing to change them in any way for fear that it will destabilize the entire Earth and cause it to crash into the sun. These classes don't share any