generics

Why doesn't C# support implied generic types on class constructors?

C# doesn't require you to specify a generic type parameter if the compiler can infer it, for instance: List<int> myInts = new List<int> {0,1,1, 2,3,5,8,13,21,34,55,89,144,233,377, 610,987,1597,2584,4181,6765}; //this statement is clunky List<string> myStrings = myInts. Select<int,string>( i => i.ToString() ). ToList<str...

Remove duplicates from a List<T> in C#

Anyone have a quick method for de-duplicating a generic List in C#? ...

Multimap in Scala

I'm trying to mixin the MultiMap trait with a HashMap like so: val children:MultiMap[Integer, TreeNode] = new HashMap[Integer, Set[TreeNode]] with MultiMap[Integer, TreeNode] The definition for the MultiMap trait is: trait MultiMap[A, B] extends Map[A, Set[B]] Meaning that a MultiMap of types A & B is a Map of types A & Set[B]...

Are there any languages that implement generics _well_?

I liked the discussion at Differences in Generics, and was wondering whether there were any languages that used this feature particularly well. I really dislike Java's List<? extends Foo> for a List of things that are Liskov-substitutable for Foo. Why can't List<Foo> cover that? And honestly, Comparable<? super Bar>? I also can't rem...

Java Generics: Comparing the class of Object o to <E>

Let's say I have the following class: public class Test<E> { public boolean sameClassAs(Object o) { // TODO halp! } } How would I check that o is the same class as E? Test<String> test = new Test<String>(); test.sameClassAs("a string"); // returns true; test.sameClassAs(4); // returns false; I can't change the method s...

Accessing non-generic members of a generic object

Is there a way to collect (e.g. in a List) multiple 'generic' objects that don't share a common super class? If so, how can I access their common properties? For example: class MyObject<T> { public T Value { get; set; } public string Name { get; set; } public MyObject(string name, T value) { Name = name; Value ...

Console.WriteLine and generic List

I frequently find myself writing code like this: List<int> list = new List<int> { 1, 3, 5 }; foreach (int i in list) { Console.Write("{0}\t", i.ToString()); } Console.WriteLine(); Better would be something like this: List<int> list = new List<int> { 1, 3, 5 }; Console.WriteLine("{0}\t", list); I suspect there's some clever way ...

Using generic classes with ObjectDataSource

I have a generic Repository<T> class I want to use with an ObjectDataSource. Repository<T> lives in a separate project called DataAccess. According to this post from the MS newsgroups (relevant part copied below): Internally, the ObjectDataSource is calling Type.GetType(string) to get the type, so we need to follow the guideline do...

Suggestions wanted with Lists or Enumerators of T when inheriting from generic classes

I know the answer is not going to be simple, and I already use a couple of (I think ugly) cludges. I am simply looking for some elegant answers. Abstract class: public interface IOtherObjects; public abstract class MyObjects<T> where T : IOtherObjects { ... public List<T> ToList() { ... } } Children: public clas...

How should I handle a situation where I need to store several unrelated types but provide specific types on demand?

I'm working on an editor for files that are used by an important internal testing tool we use. The tool itself is large, complicated, and refactoring or rewriting would take more resources than we are able to devote to it for the forseeable future, so my hands are tied when it comes to large modifications. I must use a .NET language. ...

How do I clone a generic List in Java?

I have an ArrayList<String> that I'd like to return a copy of. The ArrayList clone method has the following signature: public Object clone() After I call this method, how do I cast the returned Object back to a ArrayList<String>? ...

Are non-generic collections in .NET obsolete?

Put differently: Is there a good reason to choose a loosely-typed collection over a type-safe one (HashTable vs. Dictionary)? Are they still there only for compatibility? As far as I understand, generic collections not only are type-safe, but their performance is better. Here's a comprehensive article on the topic: An Extensive Exam...

EasyMock: How do I create a mock of a genericized class without a warning?

The code private SomeClass<Integer> someClass; someClass = EasyMock.createMock(SomeClass.class); gives me a warning "Type safety: The expression of type SomeClass needs unchecked conversion to conform to SomeClass<Integer>". ...

How to solve call ambiguity between Generic.IList<T>.this[] and IList.this[]?

I've got a collection that implements an interface that extends both IList<T> and List. public Interface IMySpecialCollection : IList<MyObject>, IList { ... } That means I have two versions of the indexer. I wish the generic implementation to be used, so I implement that one normally: public MyObject this[int index] { .... } I ...

Activator.CreateInstance(string) and Activator.CreateInstance<T>() difference

No, this is not a question about generics. I have a Factory pattern with several classes with internal constructors (I don't want them being instantiated if not through the factory). My problem is that CreateInstance fails with a "No parameterless constructor defined for this object" error unless I pass "true" on the non-public paramet...

Saving an open generic type in an array?

I am facing a problem with .NET generics. The thing I want to do is saving an array of generics types (GarphicsItem): public class GraphicsItem<T> { private T _item; public void Load(T item) { _item = item; } } How can I save such open generic type in an array? Thx ...

Can I convert the following code to use generics?

I'm converting an application to use Java 1.5 and have found the following method: /** * Compare two Comparables, treat nulls as -infinity. * @param o1 * @param o2 * @return -1 if o1&lt;o2, 0 if o1==o2, 1 if o1&gt;o2 */ protected static int nullCompare(Comparable o1, Comparable o2) { if (o1 == null) { if (o2...

Creating a Math library using Generics in C#

Is there any feasible way of using generics to create a Math library that does not depend on the base type chosen to store data? In other words, let's assume I want to write a Fraction class. The fraction can be represented by two ints or two doubles or whatnot. The important thing is that the basic four arithmetic operations are well d...

Null or default comparsion of generic argument in C#

I have a generic method defined like this: public void MyMethod<T>(T myArgument) The first thing I want to do is check if the value of myArgument is the default value for that type, something like this: if (myArgument == default(T)) But this doesn't compile because I haven't guaranteed that T will implement the == operator. So I s...

Dynamically Create a generic type for template

I'm programming WCF using the ChannelFactory which expects a type in order to call the CreateChannel method. For example: IProxy proxy = ChannelFactory<IProxy>.CreateChannel(...); In my case I'm doing routing so I don't know what type my channel factory will be using. I can parse a message header to determine the type but I hit a ...