generics

How to use method hiding (new) with generic constrained class

一个人想着一个人 提交于 2020-12-26 08:39:19
问题 I have a container class that has a generic parameter which is constrained to some base class. The type supplied to the generic is a sub of the base class constraint. The sub class uses method hiding (new) to change the behavior of a method from the base class (no, I can't make it virtual as it is not my code). My problem is that the 'new' methods do not get called, the compiler seems to consider the supplied type to be the base class, not the sub, as if I had upcast it to the base. Clearly I

In Java, can I specify any amount of generic type parameters?

Deadly 提交于 2020-12-26 05:51:28
问题 I am looking to create a particular type of interface in Java (although this is just as applicable to regular classes) . This interface would need to contain some method, say, invoke ; it would be called with a varying amount of parameters depending on the generic type arguments supplied. As an example: public interface Foo<T...> { public void invoke(T... args); } // In some other class public static Foo<Float, String, Integer> bar = new Foo<Float, String, Integer>() { @Override public void

What does it mean to instantiate a Rust generic with an underscore?

北慕城南 提交于 2020-12-25 08:27:30
问题 While working with serde_json for reading json documents, I wrote the following line of code to obtain the result of unwrapping the return value of serde_json::from_str : fn get_json_content(content_s: &str) -> Option<Value> { let ms: String = serde_json::from_str(content_s).unwrap; // <-- match serde_json::from_str(content_s) { Ok(some_value) => Some(some_value), Err(_) => None } } As you can see, I forgot the () on the end of the call to unwrap , which resulted in the following error: error

What does it mean to instantiate a Rust generic with an underscore?

空扰寡人 提交于 2020-12-25 08:25:47
问题 While working with serde_json for reading json documents, I wrote the following line of code to obtain the result of unwrapping the return value of serde_json::from_str : fn get_json_content(content_s: &str) -> Option<Value> { let ms: String = serde_json::from_str(content_s).unwrap; // <-- match serde_json::from_str(content_s) { Ok(some_value) => Some(some_value), Err(_) => None } } As you can see, I forgot the () on the end of the call to unwrap , which resulted in the following error: error

Loop over values in an IEnumerable<> using reflection

大憨熊 提交于 2020-12-25 01:39:28
问题 Given an object possibly containing an IEnumerable<T> , how would I check that an IEnumerable<T> property exists, and if it does, loop over all values in that IEnumerable<T> using reflection, for any T ? 回答1: foreach (var property in yourObject.GetType().GetProperties()) { if (property.PropertyType.GetInterfaces().Contains(typeof(IEnumerable))) { foreach (var item in (IEnumerable)property.GetValue(yourObject, null)) { //do stuff } } } 回答2: Well, you can test it as Aghilas said and, once

Loop over values in an IEnumerable<> using reflection

。_饼干妹妹 提交于 2020-12-25 01:37:39
问题 Given an object possibly containing an IEnumerable<T> , how would I check that an IEnumerable<T> property exists, and if it does, loop over all values in that IEnumerable<T> using reflection, for any T ? 回答1: foreach (var property in yourObject.GetType().GetProperties()) { if (property.PropertyType.GetInterfaces().Contains(typeof(IEnumerable))) { foreach (var item in (IEnumerable)property.GetValue(yourObject, null)) { //do stuff } } } 回答2: Well, you can test it as Aghilas said and, once

Generic implementation depending on traits

假如想象 提交于 2020-12-23 11:50:48
问题 When defining a generic struct , is there a way in Rust to use different implementation of a method according to which trait is implemented by the given generic type T ? For example: struct S<T> { value: T, } impl<T> S<T> { fn print_me(&self) { println!("I cannot be printed"); } } impl<T: std::fmt::Display> S<T> { fn print_me(&self) { println!("{}", self.value); } } fn main() { let s = S { value: 2 }; s.print_me(); } 回答1: There is an unstable feature known as specialization which permits

Generic implementation depending on traits

北城余情 提交于 2020-12-23 11:50:26
问题 When defining a generic struct , is there a way in Rust to use different implementation of a method according to which trait is implemented by the given generic type T ? For example: struct S<T> { value: T, } impl<T> S<T> { fn print_me(&self) { println!("I cannot be printed"); } } impl<T: std::fmt::Display> S<T> { fn print_me(&self) { println!("{}", self.value); } } fn main() { let s = S { value: 2 }; s.print_me(); } 回答1: There is an unstable feature known as specialization which permits

Why doesn't the type parameter of this generic function accept the given type?

半世苍凉 提交于 2020-12-15 06:52:28
问题 I want to specify the shape of some data on a subclass and a generator which creates classes with some generic T . My initial thought is to use generics as below (simplified example), but when I call makeMyClass and when I return new classRef , it gives me the following error: Type 'T' is not assignable to type 'DataInterface' Why isn't T of type DataInterface ? class MySuperClass<T> { constructor(public data: T) {} } interface DataInterface { name: string; } let initData: DataInterface = {

Generic types in RegisterClassMap with parameters

非 Y 不嫁゛ 提交于 2020-12-15 05:55:28
问题 We want a generic approach for using CsvHelper. Depending the situation, the format of the CSV file is different. When the external system has all the required info more columns are present. I need to determine if the name of column is present in the CSV file (atm this is done in ResponseMap). So we have an interface List<T> ConvertTo<T, TMap>(string file, Dictionary<string, string> headers) where T : class where TMap : ClassMap<T>; I'm calling it like this. _csvRepository.ConvertTo