generics

Why can I not return List<Entry> when the return type is IEnumerable<IEntry>?

I have: interface IEntry {} class Entry : IEntry {} Why is it that I cannot do this? IEnumerable<IEntry> func() { return new List<Entry>(); } Doesn't the returned object satisfy all the type requirements? ...

@SuppressWarnings for "extends String"

Hey guys. In a place I have a method with a generic "VT extends String". Obviously this generates a warning: The type parameter VT should not be bounded by the final type String. Final types cannot be further extended. Do you know if there's a way to suppress this warning (Eclipse)? If you're wondering how I got to have this: import jav...

Generics Default Constructor Java

public class Sample<T>{ T data; Sample(){ data = ????; } } How can i assign a default value to data ? ...

Calling generic method with Type variable

Hi SO, I have a generic method Foo<T> I have a Type variable bar Is it possible to achieve something like Foo<bar> Visual studio is expecting a type or namespace at the bar. Kindness, Dan ...

What are uses for multiply bounded type parameters in Java?

In Java generics you can use "&" to specify multiple interfaces as type bounds for a type parameter. This allows us for example to manipulate objects of different types with common interfaces to be manipulated uniformly even if there is not an parent interface for those common ones. My question is, how can this be used? For What purposes...

C# Generics Constraints: Is there a way to express is not a?

I have the following code: interface IConverter<T, U> { U Convert(T obj); } interface IBusinessEntityConveter<T, U> : IConverter<T, U> where U : BusinessEntity { } class LookupConveter<B> : IBusinessEntityConveter<Lookup, B>, IConverter<Lookup, Moniker> where B : BusinessEntity, new() { #region IConverter<Lookup, Moni...

Overriding a method contract in an extended interface that uses generics (Java)?

I am attempting to override a method declaration within an interface that extends another interface. Both of these interfaces use generics. According to the Java tutorials, this should be possible, but the example does not use generics. When I try to implement it, the compiler shows the following error (I've replaced names because some o...

"MyType" problem: Do I have to use abstract types (or generics) in Scala to return the actual class?

I am not sure if there is a better way of doing this: trait Animal { val name: String val weight: Int type SubAnimal <: Animal def updateName(n: String) = returnMe(n, this.weight) def updateWeight(w: Int) = returnMe(this.name, w) // Abstract protected method protected def returnMe(n: String, w: Int): SubAnimal } case cl...

F# type functions and a [<GeneralizableValue>] attribute

What is the difference between this two F# type functions: let defaultInstance1<'a when 'a:(new: unit->'a)> = new 'a() [<GeneralizableValue>] let defaultInstance2<'a when 'a:(new: unit->'a)> = new 'a() ...

Refactoring Similar Code Across Data Types

I am working in Java, inserting data from HashMaps of certain types into a SQL database. It has produced some code like this: for ( String key : strings.keySet() ) { result_set.updateString(key, strings.get(key)); } for ( String key : booleans.keySet() ) { result_set.updateBoolean(key, booleans.get(key)); } for ( String key : ...

C++ method-invoking template-function not able to call overloaded methods.

If you have this generic function: template<class type, class ret, class atype1, class atype2, class atype3> ret call3(type *pClass, ret(type::* funcptr)(atype1, atype2, atype3), atype1 arg, atype2 arg2, atype3 arg3) { //do some stuff here return (pClass->*funcptr)(arg, arg2, arg3); } and you do this: class MyClass { public...

How does one get the type of a generic class with multiple type parameters? - C#

This compiles: public class A<T> { public void test() { var a = typeof (A<>); } } This does not: public class A<T,S> { public void test() { var a = typeof (A<>); } } I get the error: Using the generic type 'A' requires 2 type arguments How do I get a reference to the type of this generic type with t...

Name for a specific raw type/unchecked cast combo in Java generics usage

Effective Java 2nd Edition says that we should not use raw types in new code, and we must also try to eliminate all unchecked casts warnings, and to prove and document its safety if we choose to suppress such a warning. However, I've repeatedly seen a particular usage that combines raw types and unchecked casts in a type-safe manner. In...

weird behavior around "same erasure" compilation error

I recently stumbled upon a piece of code that would not compile in my Eclipse due to the "same erasure" issue (looked very similar to this one). The guys who wrote the code assured me that it compiles in their local environment and their continuous integration and so I played along to emulate it. Take a look at this snippet: package c...

A generic List.Count gives a System.ArgumentException

I have the following class: public class SimContactDetails2G { public String AbreviatedName { get; set; } public String DialingNumber { get; set; } public int Index { get; set; } public String Surname { get; set; } public SimContactDetails2G() { } public SimContactDetails2G(String name, String phoneNum...

Generic Arithmetic in Java

Hello all, I have a filter class wherein the user must declare the type (e.g. Filter<Double>, Filter<Float> etc). The class then implements a moving average filter so objects within the class must be added. My question is how to do this? I'm sorry if the answer is simple but I've muddled myself up by thinking about it too much I think :...

Generics: Why can't the compiler infer the type arguments in this case?

I wanted to write an extension-method that would work on dictionaries whose values were some sort of sequence. Unfortunately, the compiler can't seem to infer the generic arguments from my usage of the method; I need to specify them explicitly. public static void SomeMethod<TKey, TUnderlyingValue, TValue> (this IDictionary<TKey, TVa...

Why does GetGenericTypeDefinition fail?

I have a piece of code which needs to check an entity when being saved by my Repository. I have an NHibernate interceptor on the save to check this but when I call the GetGenericTypeDefinition function the code fails with the error: [InvalidOperationException: Operation is not valid due to the current state of the object.] System.Run...

Problem instantiating generic class bean in Spring

Hi, I'm trying to instantiate a generic class in Spring, but I get following exception: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class football.dao.jpa.GenericJpaDAO]: Common causes of this problem include using a final cla...

De-Serializing a generic (but Serializable) type

I'm wondering if it's possible to de-serialize a generic type into an instance of that type. If it is possible, does Java take into account the generic type's custom de-deserialization (if any)? In my case, I'm trying to implement a List that is backed by a file which contains the serialized form of the elements, and needs to instantiat...