generics

Unity 2.0 XML configuration with generics

This is kind of a follow to my other post about Unity (http://stackoverflow.com/questions/3998559/irepository-iservice-unity-in-an-asp-net-mvc-application-reference-question). Basically I'm trying to register a generic type in the web.config. I've read a few posts and this SEEMS like it is configured correctly. <unity xmlns="http://s...

Custom marshaling TDictionary in Delphi

Hello, I need to custom marshal/unmarchal a TDictionary in Delphi (XE). The dictionary is declared as: TMyRecord = record key11: integer; key12: string; ... end; TMyDict: TDictionary<string, TMyRecord>; Now, if i marshal the dictionary without registering a custom converter, the marshaller will put all kind of fields in the JSO...

Downcasting a generic type in C# 3.5

Why can I only upcast a generic and not downcast it? How is it not clear to the compiler that if my constraint says where T : BaseClass and U is derived from BaseClass that (U)objectOfTypeT is valid? ...

How to get type parameters from SYBs dataTypeOf

Given a data type data Foo = Foo1 { foo1Name :: String} | Foo2 { foo2Name :: String, foo2Age :: Integer } I would like to be able to extract the Data.Data.DataTypeS of Foo1 and Foo2s fields. I tried datatype = (undefined :: Foo) constrs = dataTypeConstrs datatype foo1 = fromConstrs (head constrs) :: Foo foo1Fields = gmapQ dataT...

Generic static factory method problem

Hello, I have class like following: public class Tree<T extends Comparable<? super T>> { // private data omitted private Tree() {} // non parametric private constructor, not exposed // static factory method public static <T extends Comparable<? super T>> Tree<T> newInstance() { return new Tree<T>(); } } Now from anothe...

Java: T obj; type of obj.getClass() is Class<?> and not Class<? extends T>. why?

In such a function: <T> void foo(T obj) The type of obj.getClass() is Class<?> and not Class<? extends T>. Why? The following code works fine: String foo = ""; Class<? extends String> fooClass = foo.getClass(); So the signature of T#getClass() seems to return a Class<? extends T>, right? Why is the signature different if T really...

Java: what about my classOf and newArray ?

Here, I wondered about how to implement Collection#toArray(T[] array) properly. I was mostly annoyed that array.getClass().getComponentType() is of type Class<?> and not Class<? extends T> as I would have expected. Therefore, I coded these three functions: @SuppressWarnings("unchecked") static <T> Class<? extends T> classOf(T obj) { ...

C# MVC2 - Generic Collection to IEnumerable<string>

I can't seem to figure this out...a little help please, thank you vm! I have a generic collection of Features. Each Feature has a FeatureId and FeatureName. I need to pass the featureids into an IEnumerable<string>. I thought I was close with this: Listing.Features.ToArray().Cast<string>().AsEnumerable(); and even tried to 'MacGy...

What is meant by Compile-time typesafety here?

In the chapter Generics CLR Via C# v3, Jeffrey Richter says below TypeList<T> has two advantages Compile-time type safety boxing value types over List<Object>, but how is the Compile-time type safety is achieved? //A single instance of TypeList could hold different types. using System; using System.Collections.Generic; using System....

Creating generic arrays in Java

public K[] toArray() { K[] result = (K[])new Object[this.size()]; int index = 0; for(K k : this) result[index++] = k; return result; } This code does not seem to work, it will through out an exception: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to ... Could someone tell me how I can ...

How can I avoiding casting yet pass in the correct type.

I am using Entity Framework v4. I am trying to implement some logic to Validate my entities before they are saved by overriding the SaveChanges method. I am also POCO for my entities. I get a list of modified and new entities by doing the following. var entities = (ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityStat...

Enum.valueOf(Class<T> enumType, String name) question

I am trying to get around a compile error ("Bound mismatch: ...") relating to dynamic enum lookup. Basically I want to achieve something like this: String enumName = whatever.getEnumName(); Class<? extends Enum<?>> enumClass = whatever.getEnumClass(); Enum<?> enumValue = Enum.valueOf(enumClass, enumName); Whatever I do, I always end ...

Java Generic Type Variables

Hello, my question is about type variables used in generic classes and methods, why can't we do something like this T = new T(); "Construct an object of the type variable" i know that generic information is erased during compilation, and everything is converted to Object, so why doesn't the compiler assume that T is an object and let us ...

Substitution for generic type's field

I have generic structure and I need to search using various generic type's attributes. Let's think of following implementation: public class Person { private int id; private String name; // + getters & setters } Now I have my custom data structure and one of it's method is like: public T search(T data) { ... } That is nonsen...

Java Generics - Expected return type different than actual

Here is an example of some code I'm working on: public interface FooMaker<T extends Enum<T> & FooType> { public List<Foo<T>> getFoos(String bar); } Let's further assume there will be many different concrete implementations of FooMaker. So I wrote some code to utilize the FooMakers. FooMaker<?> maker = Foos.getRandomMaker(); Li...

Is there a way to get behavior similar to default(T) using reflection?

I'm refactoring some code that was originally designed with generics to also work with reflection. The particular code base has default(T) scattered throughout. I've created a method that will look similar named Default(T), but I don't think my implementation is correct. Essentially my implementation looks like this: private object D...

performing math operation upon generic variables inside of a generic Java class? how?

specificly im trying to write a piece of code that will allow me to perform basic math operations on a "T extends Number" object variable. it needs to be able to handle any number type that is a subclass of Number. i know some of the types under Number have .add() methods built in, and some even have .multiply() methods built in. i need ...

My generic type converter smells bad, exceptions at runtime, object as catch all, boxing etc.

Firstly, I've asked this question elsewhere, but meta.stackoverflow.com seems to think that asking the same questions elsewhere is fine, so here goes. I'll update both if/when I get an answer. I'm doing a lot of conversion between simple types and I wanted to handle it as nicely as possible. What I thought would be nice would be to be a...

c++ cli inhert from generic(template) interface

hello, I am writing a wrapper for a class written in C++. I must have an template interface class and inherif a wrapper class for a certain type of variable. this is the code template<typename T> public interface class IDataSeriesW { property T default [int] { T get (int Index); void set (int Index ,T val); } }; publi...

Non-generic interface as a synonym for generic one

I have one generic interface in c#, and almost always I use it with one of the types. I want to create a non-generic interface for that type and use it. Let's say, I've the following code: public interface IMyGenericList<out T> where T : IItem { IEnumerable<T> GetList(); } public class MyList<T> : IMyGenericList<T> where T : IItem...