generics

Extension methods with generics

北慕城南 提交于 2020-01-11 06:43:19
问题 I was looking at this question and i was curious why this deosn't compile. Given this code, can anyone explain why the call to IBase.Test() doesn't resolve to the correct extension method? public interface IBase { } public interface IChildA : IBase { } public interface IChildB : IBase { } public static class BaseExtensions { public static IBase Test(this IBase self) { return self; } public static T Test<T>(this T self) where T : IChildB { return self; } } public static class TestClass {

Java field type for a value of a generically recursive self-type?

心不动则不痛 提交于 2020-01-11 05:58:15
问题 Given a class hierarchy where the base class defines a recursive self-type: abstract class A<T extends A<T>> { } How can I declare another class (which should not be generic in T, because such a T could vary over the lifetime of the object) with a field that can hold any subclass of A? The following does not work: public class B { //fails to compile, because the capture of ? is not sufficiently narrow private A<?> a; public <T extends A<T>> setA(T a) { this.a = a; } } -- END OF QUESTION -- I

Java field type for a value of a generically recursive self-type?

纵然是瞬间 提交于 2020-01-11 05:58:07
问题 Given a class hierarchy where the base class defines a recursive self-type: abstract class A<T extends A<T>> { } How can I declare another class (which should not be generic in T, because such a T could vary over the lifetime of the object) with a field that can hold any subclass of A? The following does not work: public class B { //fails to compile, because the capture of ? is not sufficiently narrow private A<?> a; public <T extends A<T>> setA(T a) { this.a = a; } } -- END OF QUESTION -- I

Java using generics with lists and interfaces

和自甴很熟 提交于 2020-01-11 05:27:37
问题 Ok, so here is my problem: I have a list containing interfaces - List<Interface> a - and a list of interfaces that extend that interface: List<SubInterface> b . I want to set a = b . I do not wish to use addAll() or anything that will cost more memory as what I am doing is already very cost-intensive. I literally need to be able to say a = b . I have tried List<? extends Interface> a , but then I cannot add Interfaces to the list a , only the SubInterfaces. Any suggestions? I want to be able

Casting value to T in a generic method

不羁岁月 提交于 2020-01-11 04:29:04
问题 I have an interface for a creaky property-map: interface IPropertyMap { bool Exists(string key); int GetInt(string key); string GetString(string key); //etc.. } I want to create an extension method like so: public static T GetOrDefault<T>(this IPropertyMap map, string key, T defaultValue) { if (!map.Exists(key)) return defaultValue; else { if (typeof(T) == typeof(int)) return (T)map.GetInt(key); //etc.. } } But the compiler won't let me cast to T. I tried adding where T : struct but that

Kotlin issue “One type argument expected for class ExpandableRecyclerAdapter”

﹥>﹥吖頭↗ 提交于 2020-01-11 04:10:14
问题 Got problems with RecyclerAdapter ViewHolder abstract class ExpandableRecyclerAdapter<T : ExpandableRecyclerAdapter.ListItem>(private val context: Context) : RecyclerView.Adapter<ExpandableRecyclerAdapter.ViewHolder>() { protected var allItems = ArrayList<T>() ... open inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) open class ListItem(val itemType: Int = 0) } ExpandableRecyclerAdapter in <...> is underlined with error: One type argument expected for class

ContextualDeserializer for mapping JSON to different types of maps with Jackson

瘦欲@ 提交于 2020-01-11 03:41:05
问题 This JSON snippet should be mapped to a Java-Objects that contains a cars field of type Map<String, Car> and a bikes field of type Map<String, Bike> . Because bikes and cars can be empty strings in the JSON file, i need a custom deserializer (See this question). { "id" : "1234", "name" : "John Doe", "cars" : { "Tesla Model S" : { "color" : "silver", "buying_date" : "2012-06-01" }, "Toyota Yaris" : { "color" : "blue", "buying_date" : "2005-01-01" } }, "bikes" : { "Bike 1" : { "color" : "black"

LINQ expression with generic property

两盒软妹~` 提交于 2020-01-11 03:39:10
问题 My question is related to this one: linq-expression-with-generic-class-properties This time I would like to get newly created objects which have the id in common. The id is in fact the foreign key and therefore can have different names. I came up with the following: public static IEnumerable<T> GetNew<TId, T>(IQueryable<T> objects, TId id, DateTime date, Expression<Func<T, TId>> idSelector) where T : class, ISyncable<TId> { return objects.Where(o => idSelector(o) == id && o.CreatedDate > date

Generic Expression tree with 'OR' clause for each supplied property

此生再无相见时 提交于 2020-01-11 03:05:07
问题 I have created a generic search extension method for IQueryable that enables you to search for a single property to see if a search term is contained within it. http://jnye.co/Posts/6/c%23-generic-search-extension-method-for-iqueryable I now want to enable the user to select multiple properties to search within each, matching if any property contains the text. The code: The user enters the following code to perform this search: string searchTerm = "Essex"; context.Clubs.Search(searchTerm,

Java: how to convert a List<?> to a Map<String,?> [duplicate]

ぃ、小莉子 提交于 2020-01-11 02:07:21
问题 This question already has answers here : Java: How to convert List to Map (17 answers) Closed 4 years ago . I would like to find a way to take the object specific routine below and abstract it into a method that you can pass a class, list, and fieldname to get back a Map. If I could get a general pointer on the pattern used or , etc that could get me started in the right direction. Map<String,Role> mapped_roles = new HashMap<String,Role>(); List<Role> p_roles = (List<Role>) c.list(); for