generics

C# Property with Generic Type

I have a class: public class class1 { public string Property1 {get;set;} public int Property2 {get;set;} } Which will be instantiated: var c = new class1(); c.Property1 = "blah"; c.Property2 = 666; So bear with me (I am new to generics), I need another class with a property of a generic type so that Property1 or Property2 can be us...

Why can't I use a String array with a method taking an Iterable as a parameter?

I'm trying to write my own "functional" little lib in Java. If I have this function : public static <T> List<T> filter(Iterable<T> source,BooleanTest predicate) { List<T> results = new ArrayList<T>(); for(T t : source) { if(predicate.ok(t)) results.add(t); } return results; } why can't I use it...

List of generic interfaces

If I have a generic interface with a couple of implementing classes such as: public interface IDataElement<T> { int DataElement { get; set; } T Value { get; set; } } public class IntegerDataElement : IDataElement<int> { public int DataElement { get; set; } public int Value { get; set; } } public class StringDataElement...

Is it necessary to implement a BST with both keys and values?

Is it necessary to implement a BST with both keys and values? I can implement a BST that has method calls such as the following, in which it will make the comparison at each node of whether the traversal should go to the left node or right node based upon the V value: public class BST<V> { public void Insert(V value) { /...

Simple types with generics in Delphi

Hi How can I create a generic class only containing primitive types? TField<T: xxx> = class private FValue: T; public property Value: T read FValue write FValue; end; I don't need interfaces, classes, etc, I only want booleans, ints, floats and so on... Or there is another way to do that? Thanks ...

Pass in Type to check against

Hi everyone, I have a method in which I'm doing a check to see if the current object in a loop is the same type as a type I pass into the method. At first I was passing the type into the method as a string and then using: item.GetType().ToString().Equals(myType); What I'd really prefer to do is use the is keyword to do: item is myT...

Weird Java generic

What does this mean? HashBiMap<Character, Integer> charOcc = HashBiMap.<Character, Integer> create(); ...

Invoking statically imported method with explicit type parameters

This is the follow up of my question here: http://stackoverflow.com/questions/2050202/weird-java-generic. If I have a code like this: Casts.<X, T> cast(iterable[index]); Can I add a static import and do: <X, T> cast(iterable[index]); Eclipse doesn't allow this. But after seeing so many bugs with static import in Eclipse, I'm not t...

Passing a generic List<> in C#

I am going brain dead on this; I have several List' defined, based on specific classes (c1, c2, c3...). I have a method that will process information on these lists. What I want to do is pass in the specific list, but have the method accept the generic list, and then via typeof determine what specific work to do. I know its possible,...

My DAO's are starting to look the same, suggest a remedy design pattern?

I've noticed that having a data access object (DAO) interface and implementation is really starting to add up: public interface ForceDAO{ public void store(Force force); public void delete(Long forceId); public Force findById(Long forceId); public List<Force> findAll(); public void deleteAll(); } I have this same i...

Why implicit type inference only works in an assignment?

I know that using generic in an assignment, a method can implicitly know the type of the return type by looking at the type of the left hand side variable. Example from Google Collection: List<String> l = Lists.newArrayList() My question is why it doesn't work for a method or higher type of inference? Example: List<List<String>> ll...

IEnumerable and Recursion using yield return

Hi everyone, I have an IEnumerable<T> method that I'm using to find controls in a WebForms page. The method is recursive and I'm having some problems returning the type I want when the yield return is returnig the value of the recursive call. My code looks as follows: public static IEnumerable<Control> ...

How to instantiate generic variable like Class<Foo<T>>?

I need to instantiate generic variable like Class>. For example, Class<? extends List<String>> = ... Variant 1: Class<? extends List<String>> clazz = LinkedList.class; don't work - "Incompatible types". Variant 2: Class<? extends List> clazz = LinkedList.class; work, but in this case List is a raw type - not good. How to insta...

java generics and SuppressWarnings

I have classes abstract class A { //.... } class B extends A { //.... } class C extends A { //.... } Then I have interface Maker<T extends A> { SomeClass make(T obj); } implementations for Maker class class MakerForB implements Maker<B> { /*... */ } class MakerForC implements Maker<C> { /*... */ } and class F...

Circular type parameters definition in scala

I am trying to define a generic container whose elements can return the enclosing container. Something like: abstract class Container[E <: Element] { // compile error def contains( e: E ): Boolean def addNewElement(): Unit } abstract class Element[C <: Container] { // compile error def enclosingContainer(): C } class MyContainer...

StructureMap setter injection in open generic type?

Using StructureMap, I'm trying to use setter injection on an open generic type. I have an abstract generic class: public abstract class Foo<T1, T2> : IMyInterface<T1,T2> { public ISomeDependency Bar { get; set; } } I want to use Setter Injection to resolve "Bar" on any inheritors of Foo. I know I can do this using the [SetterDepe...

MethodInfo, CreateDelegate and Generic Methods

Thanks to Jon Skeet's answer in this question I have the following working: public delegate BaseItem GetItemDelegate(Guid itemID); public static class Lists { public static GetItemDelegate GetItemDelegateForType(Type derivedType) { MethodInfo method = typeof(Lists).GetMethod("GetItem"); method = method.MakeGenericMethod(new...

A constrained generic delegate in C#

I would like to have a delegate that is constrained to returning one of two types; an ActionResult or a string. Is this possible? ...

Java generics extending return type of methods

Take a look at these three classes. Minatchi allows itself to be extended so that its methods' returning type could be extended as well. To illustrate, I used a static method. public class Minatchi<T extends Minatchi<?>>{ static public <T extends Minatchi<?>> List<T> listAll(){ return (List<T>) query(); } } And ...

What does this class declaration mean in Java?

I just learn up to tree and one thing I don't understand about it is the class declaration: for example: class BinarySearchTree<T extends Comparable<? super T>>. Now, can you please explain me what is in the bracket and the "<? super T>"? Any good source you can refer me to? Thanks. ...