generics

What about optional generic type parameters in C# 5.0?

Just a thought. Wouldn't it be useful to have optional type parameters in C#? This would make life simpler. I'm tired of having multiple classes with the same name, but different type parameters. Also VS doesn't support this very vell (file names) :-) This would for example eliminate the need for a non-generic IEnumerable: interface ...

What would be the use of accepting itself as type arguments in generics

I saw some code on an unrelated question but it got me curious as I never saw such construct with Java Generics. What would be the use of creating a generic class that can take as type argument itself or descendants of itself. Here is example : abstract class A<E extends A<E>> { abstract void foo(E x); } the first thing that cam...

How do i assigned the type of any object to a .NET generic method?

Suppose that I have created a method like this private void Test<t>(t str) { } Now from another method i call it private void BtnClick() { string a = ""; test<Here I Want To assign The Type what ever the type of 'a' is>(); } How can I do this ? ...

Extending both T and SomeInterface<T> in Java

I want to create a class that takes two parameters. One should be typed simply as T. The other should be typed as something that extends both T and SomeInterface<T>. When I attempt this with public class SomeClass<T, S extends SomeInterface<T> & T> then Java complains with "The type T is not an interface; it cannot be specified a...

Java Generics name clash, method not correctly overriden

Hi. I have seen different questions regarding this, but I still find this topic to be very confusing. All I want to do, is have an abstract class that implements an interface, and have a class extending this abstract class so that the hard class needs to implement getKommune() and setKommune(Kommune kommune), but not the other method, ...

Instructing a generic to return an object of a dynamic type

This question is sort of a follow-up to my original question here. Let's say that I have the following generic class (simplifying! ^_^): class CasterClass<T> where T : class { public CasterClass() { /* none */ } public T Cast(object obj) { return (obj as T); } } Which has the ability to cast an object into a s...

Using string[] as a Dictionary key e.g. Dictionary<string[], StringBuilder>

The structure I am trying to achieve is a composite Dictionary key which is item name and item displayname and the Dictionary value being the combination of n strings So I came up with var pages = new Dictionary<string[], StringBuilder>() { { new string[] { "food-and-drink", "Food & Drink" }, new StringBuilder() }, { new string...

Generic foreach loop in C#.

The compiler, given the following code, tells me "Use of unassigned local variable 'x'." Any thoughts? public delegate Y Function<X,Y>(X x); public class Map<X,Y> { private Function<X,Y> F; public Map(Function f) { F = f; } public Collection<Y> Over(Collection<X> xs){ List<Y> ys = new List<Y>(); ...

How to make safe cast using generics in C#?

I want to implement a generic method on a generic class which would allow to cast safely, see example: public class Foo<T> : IEnumerable<T> { ... public IEnumerable<R> SafeCast<R>() where T : R { return this.Select(item => (R)item); } } However, the compiler tells me that Foo<T>.SafeCast<R>() does not d...

Typed DefaultListModel to avoid casting

Is there a way in java to have a ListModel that only accepts a certain type? What I'm looking for is something like DefaultListModel<String> oder TypedListModel<String>, because the DefaultListModel only implements addElement(Object obj) and get(int index) which returns Object of course. That way I always have to cast from Object to e....

Create method to handle multiple types of controls

I am trying to create a method that accepts multiple types of controls - in this case Labels and Panels. The conversion does not work because IConvertible doesn't convert these Types. Any help would be so appreciated. Thanks in advance public void LocationsLink<C>(C control) { if (control != null) { Web...

How do I get the type argument of a generic type?

i think this is a simple question but I've searched around and can't seem to find an answer easily. if you have var list = List<int>(); ... fill list ... and you want to get the generic type in list, i realise you could just type: var t = list.FirstOrDefault().GetType(); Is there another way to do this via just the list, rather th...

c++ global operator not playing well with template class

ok, i found some similar posts on stackoverflow, but I couldn't find any that pertained to my exact situation and I was confused with some of the answers given. Ok, so here is my problem: I have a template matrix class as follows: template <typename T, size_t ROWS, size_t COLS> class Matrix { public: template<typename, ...

How to create a generic C free function .

I have some C structures related to a 'list' data structure. They look like this. struct nmlist_element_s { void *data; struct nmlist_element_s *next; }; typedef struct nmlist_element_s nmlist_element; struct nmlist_s { void (*destructor)(void *data); int (*cmp)(const void *e1, const void *e2); unsigned int size; ...

Changing the Order in a .NET Generic Dictionary

I have a class that inherits from a generic dictionary as follows: Class myClass : System.Collections.Generic.Dictionary<int, Object> I have added a list of values to this in a particular order, but I now wish to change that order. Is there any way (without removing and re-adding) that I could effectively re-index the values; so chan...

Create a Generic IEnumerable<T> given a IEnumerable and the member datatypes

Hi, I get an IEnumerable which I know is a object array. I also know the datatype of the elements. Now I need to cast this to an IEnumerable<T>, where T is a supplied type. For instance IEnumerable results = GetUsers(); Type t = GetType(); //Say this returns User IEnumerable<T> users = ConvertToTypedIEnumerable(results, t); I now wan...

How can I define an anonymous generic Scala function?

Let's say I have this: val myAnon:(Option[String],String)=>String = (a:Option[String],defVal:String) => { a.getOrElse(defVal) } Don't mind what the function does. Is there anyway of making it generic, so I can have an Option[T]? ...

java casting a list

Let's say I had: protected void performLogic(List<Object> docs) { ... } In the code where I'm going to be calling this method, I have a List<String> list. I want to call performLogic, passing this list. But it won't work because the lists are 2 different types: public void execute() { List<String> docs = new ArrayList<String>()...

Binary comparison operators on generic types

I have a generic class that takes a type T. Within this class I have a method were I need to compare a type T to another type T such as: public class MyClass<T> { public T MaxValue { // Implimentation for MaxValue } public T MyMethod(T argument) { if(argument > this.MaxValue) { ...

Why declare "private List contactInfos" without a generic ("private List <ContactInfo> contactInfos") ?

In this example from the App Engine docs, why does the example declare contactInfos like this (no Generics): import javax.jdo.annotations.Element; // ... @Persistent @Element(dependent = "true") private List contactInfos; instead of like this, using a Generic: import javax.jdo.annotations.Element; // ... @Persistent ...