generics

Java: specific enums and generic Enum<?> parameters

Hi all, I want to pass any enum value to method in utility class and get another enum value of same enum type. Something like this: public class XMLUtils { public static Enum<?> getEnumAttribute(Element element, String name, Enum<?> defaultValue) { if (element.hasAttribute(name)) { String valueNam...

Java - what is called again: <String> <>

What are these called? List<String> ids = new ArrayList<String>(sectionIDs); ( the part that is <> ) I know it's a newer java feature, I just cant think of the name. ...

Generics in VB.NET

Now, as a C# programmer, I know that generics are awesome. However, when dabbling in some VB.NET, I discovered that the following does not cause a compiler error: Dim instance As List(Of Integer) instance.Add(True) Why is this? I know that you are not required to cast in VB.NET, but I'd have thought that this kills the main reason to...

How to get generic's class

Class Model<T>{ private T t; ..... private void someMethod(){ //now t is null Class c = t.getClass(); } ..... } Of course it throws NPE. Class c = t.getClass(); What syntax should i use to get class of T if my instance is null? Is it possible? ...

Does F# have generic arithmetic support?

Does F# have the same problem as C# where you can't directly use arithmetic operators with generic T types? Can you write a generic Sum function that would return the sum of any value that supports the arithmetic addition? ...

Getting T.class despite Java's type-erasure

I'm trying to bind an interface to its implementation as read from a configuration file so that I can feed it to my IoC container. Here's roughly what I'm trying to do: public class PropertyImplementationBinder<T> { // ... public Class getInterfaceClass() { return T.class; // OR Class<T>, note T is not newable } ...

How to design a persistent object where a generic type would be greatly helpful.

I'm creating an object that will be used to store 'known quantities' or facts. Ideally, it'd look something like: public class KnownQuantity<T> { public T Quantity { get; set; } public KnownQuantity(T quantity) { this.Quantity = quantity; } } However, this class is to become a persistent object. Currently I'm...

Java generics and JNI

Hi, Is it possible to call a native CPP function using JNI which takes generic arguments? Something like the following: public static native <T, U, V> T foo(U u, V v); And then call it like: //class Foo, class Bar, class Baz are already defined; Foo f = foo(new Bar(), new Baz()); Can anyone please provide me with a sample which is...

Variable length arguments c#

This is probably not possible, but here goes: I want to create a struct where I can define the amount of arguments at declaration. for example, now I am using: KeyValuePair<T, T> but KeyValuePair can only ever take a Key , and a Value . Is it possible to make something like: CustomValues<T, {T, {..}}> I think this isn't possible...

Generics and Java : how does "extends" work ?

I am trying to write a Generic Data Access Layer for my application. I have multiples hibernate entities which are mostly the same and are represented as a class hierarchy in Java (they are not implemented as a hierarchy in Hibernate) : public abstract class Entity1 { // some implementation } public class Entity2 extends Entity1 { ...

How to convert an anonymously typed List to List<T>?

This simple Linq query: from c in mycontext.Customers join o in mycontext.Orders on c.CustomerId equals o.CustomerId where o.Status == 1 select new {c, o} will result in List<{c:Customer, o:Order}> after calling ToList(). What's the easiest way of converting this anonymously typed list into a list of customers (List<Customer>)? E...

Help with C# generics error - "The type 'T' must be a non-nullable value type"

I'm new to C# and don't understand why the following code doesn't work. public static Nullable<T> CoalesceMax<T>(Nullable<T> a, Nullable<T> b) where T : IComparable { if (a.HasValue && b.HasValue) return a.Value.CompareTo(b.Value) < 0 ? b : a; else if (a.HasValue) return a; else return b; } // Sample...

Visual Studio load design form

Does anyone came across a problem when you have a Class that inherits from Forms and then have all the forms inherit from Class, and then tried to load the form in design mode in visual studio? Does it loads correctly, because on mine it shows "Illegal characters in path.". I ran the visual studio in debug mode and found that it is try t...

Upcasting with a generic type parameter

Is it possible to "upcast" from a generic class based on T, into a generic class based on something more general than T? For instance, say I have a class named Derived that inherits from a class named Base. Can I ever do something like this: List<Derived> der = new List<Derived>(); List<Base> bas = (List<Base>) der; Or, using interfa...

How do I sort a generic list based on a custom attribute?

I am working in c#.NEt 2.0. I have a class, let's say X with many properties. Each properties has a custom attribute, an interger, which I planned to use to specify its order int he final array. Using reflection I read through all of the properties and group the values and place them into a generic list of properties. This works and I c...

java interface extends comparable

Hi all, I want to have an interface A parameterised by T A<T>, and also want every class that implements it to also implement Comparable (with T and its subtypes). It would seem natural to write interface A<T> extends Comparable<? extends T>, but that doesn't work. how should I do it then? ...

I Use Generics, But Not This Class<T> thing!

I am trying to call this method to concat two arrays using Google Collections public static <T> T[] concat(T[] first, T[] second, Class<T> type) It's returning empty results. I am using ObjectArrays.concat(array1, array2, Blah.class) which is the only thing that compiles. a...

Java generics and type erasure

Given the following code: public void example(Object o) { if(o instanceof List<MyType>) //do something } I understand that this is not possible (and why its not possible) given the way Java handles generics and type erasure. My question is, what is the best/cleanest way to accomplish this? Or is the only thing I can do is check...

Can Not Compare Generic Values

I am creating a generic class to hold widgets and I am having trouble implementing the contains method: public class WidgetBox<A,B,C> { public bool ContainsB(B b) { // Iterating thru a collection of B's if( b == iteratorB ) // Compiler error. ... } } Error: Operator '==' cannot be applied to operan...

using generics in stateless session bean exposed as a webservice

I am trying to use generic request and repsonse classes in the a stateless session bean method exposed as a webservice. Scenario is 1)Request object that takes a generic type. i.e. Request<T> Now i use this object as the input parameter for a stateless session bean method. i.e. signature of the method is . Response<Employee> createE...