generics

Generics question

I have a generic class public class Decoder<SIGNAL> where SIGNAL : signalType, new() signalType is a abstract class. How do I declare a dynamic field to store it? The following code will throw a compile error saying that Decoder must be a non abstract type generic. public class DecoderParent { private Decoder<signalType> decode...

What's the difference between functors and "generics"

I'm looking at OCaml's functors. It looks to me pretty identical to the so called generic objects in C++/C#/Java. If you ignore Java's type erasion for now, and ignore the implementation details for C++ templates (I'm interested with the language feature), functors are quite indentical to generics. If I understand it correctly, functor g...

Why can't a Generic type containing a Generic type be assigned to a Generic typed class of wildcard type

Sorry if the title seems confusing, but some examples are in order. Let's say I have some Java class with a generic type parameter: public class GenericClass<T> { } I can create a variable typed to store an object, with the generic parameter set to, say a String. Java will also let me assign that variable to another variable but with...

Calling a method on an object the type of which I don't know

I have a generic PostProcessor interface that looks like this: public interface PostProcessor<T> { public void process( T t ); } I'm implementing a PostProcessor for generic lists that will call an appropriate postprocessor for each item in the list. public class ListPostProcessor implements PostProcessor<List<?>> { public vo...

Need help in passing Java Generics to Collections.sort()

Hi, I need help in passing a Java Generics List to Collections.sort(). Here is my code: List<MyObject> orgMyObjectList = new ArrayList<MyObject>(); Collections.sort(orgMyObjectList, new Comparable<MyObject>() { public int compareTo(MyObject another) { // TODO Auto-generated method stub return 0; ...

Where can I find the source code of C++'s generic.h?

At present, I'm using a C-like language (NXC) to control a LEGO MINDSTORMS robot. That C-like language doesn't have support for generics (or, not being C++, classes. But I digress). However, I read in an outdated C++ book that either C or C++ has a file called generic.h, which contains helpful preprocessor macros (ie name2 and declare) f...

Why does the runtime shows the generic types as "GenericType`n"?

And why doesn't it shows the real type (eg: List<string> instead of List`1)? Where does this strange (to me) notation comes from? ...

Purpose of <T, TU, etc.>

Hi. I honestly do not know what they are called. I have been unable to find articles on the internet. So I understand that you can do something like this: public struct Pair<T, U> { public readonly T Value1; public readonly U Value2; public Pair(T fst, U snd) { this.Value1 = fst; this.Value2 = snd; ...

How to require certain concepts in C++ code?

How do I require and check that an argument is a certain concept in C++? For example, the random_shuffle function in the algorithm header requires that its arguments are RandomAccessIterators: template<typename _RandomAccessIterator> inline void random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last) {...

ASP.NET MVC Model Binder for Generic Type

Is it possible to create a model binder for a generic type? For example, if I have a type public class MyType<T> Is there any way to create a custom model binder that will work for any type of MyType? Thanks, Nathan ...

Set property Nullable<> by reflection

I try to set a Nullable<> property dynamicly. I Get my property ex : PropertyInfo property = class.GetProperty("PropertyName"); // My property is Nullable<> at this time So the type could be a string or int I want to set my property by reflection like property.SetValue(class,"1256",null); It's not working when my property is a Nu...

C# Generics - Determine generic type from consuming generic method

Long story: I'm working on a system that will have Tasks and Handlers. Tasks will be sent from disparate parts of the system to the Handlers, where they will eventually return a value. We've got a system in-place that is loosely-typed and has turned into something of a gnarled mess (the problem is significantly amplified by the fact ...

How do I setup multiple type bounds in Scala?

I want to be able to declare something like this: trait Narrowable[A] extends Iterable[A] { def narrow[B <: A & B <: AnyRef] : Iterable[B] } That it, the type B should be both a subtype of A and AnyRef. Is this possible? ...

java generics and collection assignment

If I have this class: class Foo<T> implements SomeInterface { final private List<T> list = new ArrayList<T>(); final private Class<? extends T> runtimeClass; public Foo(Class<? extends T> cl) { this.runtimeClass = cl; } // method override from SomeInterface @Override public boolean addChild(Object o) { ...

Sorting List<MyObject>

Which is the quickest way in Java to sort a list List<MyObject> myObjectList based on some numerical property of MyObject? ...

How does one instantiate an array of maps in Java?

I can declare an array of maps using generics to specify the map type: private Map<String, Integer>[] myMaps; However, I can't figure out how to instantiate it properly: myMaps = new HashMap<String, Integer>[count]; // gives "generic array creation" error myMaps = new HashMap[count]; // gives an "unchecked or unsafe operation" warnin...

How to pass a Generic class as a Parameter to a non-generic class constructor

Assume the following code (Please read my question in the code comments in the final class): //This is my Generic Class public class ClientRequestInfo<K, V> { public string Id { get; set; } private Dictionary<K, V> parameters; public ClientRequestInfo() { parameters = new Dictionary<K, V>(); } public vo...

Problem with generics in Java

I'm sure that my problem is common and I almost sure that it doesn't have easy solution. So: I have an interface: public interface Task<E> extends Serializable { Task<E>[] splitTask (int partsNum); E mergeSolutions (E... solutions); E solveTask (); E getSolution (); Integer getId (); void setId (Integer id); } ...

How to solve this generic with repository pattern problem?

I had this code from a previous question, but its not compiling: public interface IEntity { // Common to all Data Objects } public interface ICustomer : IEntity { // Specific data for a customer } public interface IRepository<T, TID> : IDisposable where T : IEntity { T Get(TID key); IList<T> GetAll(); void S...

Really bizarre C# generics question

This code compiles but looks very strange. I have a typical and simple parent/child relationship here which is implemented using generics in a very strange way. But I can't seem to find any other way of doing it. class SampleObject<T> //I don't want to make this a generic but am forced to { //The SampleContainer this object is in //...