generics

Can you declare a variable length Generics type declaration?

I have an overloaded utility method called CheckDuration with following function signatures. private static Action<int> CheckDuration(Action action) private static Action<int> CheckDuration<T>(Action<T> action, T arg) Basically CheckDuration prints on console how long it took to run a method. Now, I would like to check the du...

When is it Appropriate to use Generics Versus Inheritance?

What are the situations and their associated benefits of using Generics over Inheritance and vice-versa, and how should they be best combined? Thanks for the answer guys. I'm going to try to state the motivation for this question as best I can: I have a class as shown below: class InformationReturn<T> where T : Info { InformationR...

Defining spring bean using a class with generic parameters

If I have a class that looks something like this: public class MyClass<T extends Enum<T>> { public void setFoo(T[] foos) { .... } } How would I go about declaring this as a bean in my context xml so that I can set the Foo array assuming I know what T is going to be (in my example, let's say T is an enum with the values ONE and...

Java Generic Question: Any better way?

[Update]: my initial example doesn't reflect my problem. Updated the sample, hopfully it is better now. Java Generic and overloading don't play well together. public interface Request<E> { E getValue(); } I have a generic parameterized Request interface as above, and I would like write a DataStore class to save the payload request ...

Create Custom Hashtable

Hi there.. I need to create a Custom Hashtable extends java.lang.Hashtable and i need to override the get method to achieve the following behavior : if the key == null, it will return a new object of the type V if the super.get(key) == null, it will also return a new object of type V. Can anyone help me. I try to do this but I know ...

Deriving Class from Generic T

I have a parameterized hibernate dao that performs basic crud operations, and when parameterized is used as a delegate to fulfil basic crud operations for a given dao. public class HibernateDao <T, ID extends Serializable> implements GenericDao<T, ID> I want to be able to derive Class from T at runtime to create criteria queries in Hi...

Size of generic type

Is there any way to determine the size in bytes of something like TItem <T> = record Data : T; end; Can I write something like function TItem <T>.GetByteSize : Integer; begin if (T = String) then Result := GetStringByteSize (Data as String) else Result := SizeOf (Data); end; or perhaps with the help of specialization? functi...

Struggling to come up with a generic C# method that compares different types of numeric objects

I have an Infragistics UltraNumericEditor (version 5.3, quite old) control on a form. If I set the .Value field to something less than .MinValue or something more than .MaxValue then I get a System.Exception thrown with the following message: The 'Value' property cannot be set to a value that is outside the range determined by the ...

Trouble defining an IList<T> where T is an generic interface

I am attempting to declare and use an interface like this: public interface IItem<T> { string Name { get; set; } T Value { get; set; } } This works fine until I attempt to create a list of these items. This fails to compile: public interface IThing { string Name { get; } IList<IItem<T>> ThingItems { get; } } so I am no...

Java Generics and Infinity (Comparable)

With the type Integer you can do this: int lowest = Integer.MIN_VALUE; What can I do if I use generics? K lowest = <...>; I need this in order to implement something similar to a PriorityQueue. I have access to a node I want to remove from the queue, but it is not the min. 1. I need to make it the min by decreasing the key of that...

Convention for Filenames of Generic Classes

I want to be able to distinguish between a generic and regular (non-generic) version of a class. Much like the .NET framework does with it's generic and non-generic versions of several of it's interfaces and collection classes. (Queue, Queue(T)) I generally like to follow the convention of one class per file (as in Java). Is there a com...

Generics Question

I would have made the title more specific but I don't know how to put it. I'm trying to infer the type of a generic's generic. public class BaseAction<T> { public virtual void Commit(T t1, T t2){ //do something }; } public class SpecificAction : BaseAction<int> { // I would have specific code in here dealing with ints // publ...

Java: Cyclic generic type relation doesn't allow cast from supertype (javac bug).

Hello! I encounter a totally strange behavior of the Java compiler. I can't cast a supertype to a subtype when cyclic generic type relation is involved. JUnit test case to reproduce the problem: public class _SupertypeGenericTest { interface ISpace<S extends ISpace<S, A>, A extends IAtom<S, A>> { } interface IAtom<S extends ISpac...

Can I inherit from a generic class without specifying a type?

I have the following sample code in a VB.NET console application. It compiles and works, but feels like a hack. Is there a way to define EmptyChild so that it inherits from Intermediate(Of T As Class) without using the dummy EmptyClass? Module Module1 Sub Main() Dim Child1 = New RealChild() Child1.Content = New RealClass() ...

How to define generic type limit to primitive types?

I have the following method with generic type: T GetValue<T>(); I would like to limit T to primitive types such as int, string, float but not class type. I know I can define generic for class type like this: C GetObject<C>() where C: class; I am not sure if it is possible for primitive types and how if so. ...

What is the syntax for creating a Dictionary with values as instances of Action<T> ?

I need to create a Dictionary object as a member field key = string value = an instance of Action<T> where T could be different per entry, e.g. long, int, string (a ValueType or a RefType) However can't get the compiler to agree with me here.. To create a member field it seems to need a fixed T specification. I've passed my limit of str...

Conditional behaviour based on concrete type for generic class

Since my question from yesterday was perhaps not completely clear and I did not get the answer I wanted, I will try to formulate it in a more general way: Is there a way to implement special behaviour based on the actual type of an instantiated generic type either using explict conditional statements or using some kind of specialization...

How to exit a Generic list ForEach with delegate ?

How do i exit a Generic list ForEach with a delegate? Break or return doesn't work. Example: Peoples.ForEach(delegate(People someone) { if(someone.Name == "foo") ???? What to do to exit immediatly ? }); ...

Generic container for exposing POCO instances to other AppDomains - how does it work?

I'm intrigued by this answer from another SO thread, and I was hoping someone can help me shine some light on the concept. Say I have a primary AppDomain and a bunch of child AppDomains, that are created and initialized by the primary AppDomain. In pseudo code: Primary AppDomain: class Parent { public void InitChildren(IList<Child...

Interface with a list of Interfaces

Maybe I'm dumb but ... I have: public interface IRequest { IList<IRequestDetail> Details { get; set; } // stuff } public interface IRequestDetail { // stuff } I then have: public class MyRequest : IRequest { IList<MyRequestDetail> Details {get; set; } // stuff } public class MyRequestDetail : IRequestDetail {...