generics

Using is operator with Interface to Generics

I have a few generic classes that implement a common non-generic interface. I create my generic objects and add them to a list. How can I use LINQ, or any other method for that matter, to filter the list by the generic type. I do not need to know T at run-time. I added a type property to the interface and used LINQ to filter by it but I ...

How to get a base class method return type to be the subclass type?

I have a copy function that I'd like to override in subclasses to return the type of the subclass. Here are my interfaces: Public Interface IBase(Of T) Function Copy() As T End Interface Public Interface ICar Inherits IBase(Of ICar) End Interface Public Interface IToyota Inherits ICar End Interface And here are my classe...

Can you specify generics type on map value?

In Java (5+) you can do: TreeMap<String, List> map = someObject.getMap(); But can specify the generic type within the List? A la: TreeMap<String, List<String>> map = someObject.getMap(); ?? ...

Is it possible to enforce that a type param is nullable on a class

given a class definition like: public class Test<T> { T _value; public void Test(T value) { _value = value; } public void DoStuff() { if(_value.HasValue) { //stuff } } } I would like to enforce that T is nullable so I can use the class like: //does stuff new T...

Arrays in Generic class

Is there any disadvantages in using array in generic class? If yes, what are they? If No what are the advantages? ...

DataTable into List<T> *without* where T : class, new() - potential problems?

Hi all, I've been looking on SO this morning to find a way of creating a List of specific objects from a System.Data.DataTable. So far, I have this in my DomainModel base class: protected static List<T> ConvertTo<T>(DataTable dt) where T : class, new() { int count = dt != null ? dt.Rows.Count : 0; List<T> list =...

Why does this use of Java Generics not compile?

For the life of me I cannot understand why the compiler won't let me do the following... import java.util.HashMap; import java.util.Map; public class TestMap { private final Map<Integer, ? extends Number> map = new HashMap<Integer, Number>(); public void put(Integer key, Long item) { this.map.put(key, item); } } Why d...

Generic method with Action<T> parameter

So, I'm sure this has been answered somewhere out there before, but I couldn't find it anywhere. Hoping some generics guru can help. public interface IAnimal{} public class Orangutan:IAnimal{} public void ValidateUsing<T>(Action<T> action) where T : IAnimal { Orangutan orangutan = new Orangutan(); action...

Generics in Java

I'm new to this community so I just wanted to start off by saying Hi. On to my question: I came across something peculiar today, take a look at this code snippet List <Rectangle> test1 = new LinkedList<Rectangle>(); List <Shape> test2 = test1; //Compiler Error; This is of course assuming that class Rectangle is a subclass of Shape. C...

Java Generics comparison

How do you go about comparing two generic classes? class Entry<K,V> { protected K key; protected V value; public K getKey() { return key; } public V getValue() { return value; } public static Comparator KeyComparator = new Comparator() { public int compare(Object o1, Object o2) { int key1 = ( (Entry) o1 ).getKey(); int...

Define an anonymous structure as a parameter in a method

Can I define an object-structure as a parameter to a method in the parameter declaration without having to create a type? I am inspired by LINQ to SQL queries, where you are able to return a subset of your query-results in the form of a new object: var query = from t in dc.Table select new { Foo = t.column }; ...

How to add an item to a list of generics declared as a list of an abstract object in C#

Hello everybody. So I have an abstract class named "Account" : public abstract class Account { private string _FinancialInstitution; public string FinancialInstitution { get { return _FinancialInstitution; } ... } } And I have two other classes that extends from those two: public class CreditCard : A...

What's wrong with my generic method?

the following generic method gives the compile error "Error 13 Value of type 'Distribution' cannot be converted to 't'" : Public Shared Function CreateObject(Of t)(ByVal ID As Integer) As t Dim TType As Type = GetType(t) If TType Is GetType(Distribution) Then Return CreateDistribution(ID) ElseIf TT...

C#: Generic interfaces and understanding "type parameter declaration must be an identifier not a type"

I'm trying to understand the generic interface as described in this My example has an interface: public interface ITest<T> where T: class { T GetByID(int id); } I have a class that implements the interface, using LINQ to enties in project Data, which contains the class myClass: public class Test<myClass> : ITest<myClass...

Why do several java.util.List methods not use the type parameter?

Possible Duplicate: What are the reasons why Map.get(Object key) is not (fully) generic This is similar to the question here, on java.util.Map. This question is left as a pointer to that question. The List interface contains several methods which still accept an Object as parameter, after generics were introduced in Java 5. S...

Type inference: Using generic methods with implicit type conversion

The problem is that you want to flatMap a List[Option[T]] to a List[T] : val l = List(Some("Hello"), None, Some("World")) to get: List(Hello, World) but there is no nice solution: l flatMap( o => o) l flatMap identity[Option[String]] l flatMap ( x => Option.option2Iterable(identity(x))) for(x <- l; y <- x) yield y The obvious so...

How to use an Interface as Map’s Key

I am looking for help on the subject how to use an Interface as Maps Key. I tried to implement a solution, and get no compiletime errors but runtime errors when running my integration tests. Is it not possible to use an Interface as a Key, or is it my tests there is something wrong with? My code looks something like this private Map<AI...

Why is flatten declared on GenericTraversableTemplate and not TraversableLike?

The signature of TraversableLike.flatMap is as follows: def flatMap[B, Th](f : (A) => Traversable[B])(implicit bf : CanBuildFrom[Repr, B, Th]) : Th The signature of GenericTraversableTemplate.flatten is: def flatten[B](implicit asTraversable : (A) => Traversable[B]) : CC[B] Why is the latter method (which seems to me to differ from...

The same property name but different return type in a generic list

Beginner's question: How to implement a generic list List<Item> returning items with a property named Data but returning different types for different subclasses? I started building the following hierarchy, but that does not lead to the goal. abstract class Item abstract class ItemGeneric<TData> : Item class ItemText : ItemGeneric<strin...

The type Collection is not generic; it cannot be parameterized with arguments <? extends E>

Hi! I have a strange problem with eclipse galileo. I set java 1.6 as jre. And for this line of the code List templates = new ArrayList (); I see such error in eclipse problem list: The type Collection is not generic; it cannot be parameterized with arguments <? extends E> But I don't have any problems with building this project wit...