generics

C++/CLI value class constraint won't compile. Why?

Hello, a few weeks ago a co-worker of mine spent about two hours finding out why this piece of C++/CLI code won't compile with Visual Studio 2008 (I just tested it with Visual Studio 2010... same story). public ref class Test { generic<class T> where T : value class void MyMethod(Nullable<T> nullable) { } }; The ...

Why Is It That Generics Constraint Can't Be Casted to Its Derived Type?

It is quite puzzling to find out that Generics Constraint Can't Be Casted to Its Derived Type. Let's say I have the following code: public abstract class BaseClass { public int Version { get { return 1; } } public string FixString { get; set; } public BaseClass() { FixString = "hello"; } public vi...

On Joining TObjectlists

I think i need a nudge in the right direction: I have two Tobjectlists of the same datatype, and i want to concatenate these into a new list into which list1 shall be copied used unmodified followed by list2 reversed) type TMyListType = TobjectList<MyClass> var list1, list2, resList : TMyListtype begin FillListWithObjects(list1...

How created method with signature as List

Hi all, I'm very new to Java programming language so this is probably dumb question but I have to ask it because I can't figure it out on my own. Here is the deal. I want to create method which extracts certain object type from a list. So the method should receive List as argument, meaning list should contain either Object1 or Object2....

Calling a Generic Method using Lambda Expressions (and a Type only known at runtime)

You can use Lambda Expression Objects to represent a lambda as an expression. How do you create a Lambda Expression Object representing a generic method call, if you only know the type -that you use for the generic method signature- at runtime? For example: I want to create a Lambda Expression Objects to call: public static TSource L...

[Java/OO] Question about the design of dynamic subclass hierarchy handling.

I always tend to run into the following design problem that I'm never quite sure how to best resolve. It usually starts with a hierarchy of Animals at my Circus: Animal Cat BigCat Dog Elephant ... Now, each Animal needs to be trained, so that there is a separate method for each: public interface Trainer { v...

Java Builder pattern with Generic type bounds

Hi all, I'm attempting to create a class with many parameters, using a Builder pattern rather than telescoping constructors. I'm doing this in the way described by Joshua Bloch's Effective Java, having private constructor on the enclosing class, and a public static Builder class. The Builder class ensures the object is in a consistent...

How can I make the C# compiler infer these type parameters automatically?

I have some code that looks like the following. First I have some domain classes and some special comparators for them. public class Fruit { public int Calories { get; set; } public string Name { get; set; } } public class FruitEqualityComparer : IEqualityComparer<Fruit> { // ... } // A basket is just a group of Fruits. public c...

Passing a generic class to a java function?

Is it possible to use Generics when passing a class to a java function? I was hoping to do something like this: public static class DoStuff { public <T extends Class<List>> void doStuffToList(T className) { System.out.println(className); } public void test() { doStuffToList(List.class); ...

Holding value in collection

I have a application which is on timesheet. I have total of 54 columns out of which 10 columns are visible rest invisible. First 3 columns are Project, MileStone and Classes. Rest are Sun- Sat work hrs, TaskId, TaskDesc and so on for each day. On my grid only first 3 columns and Sun - Sat work hrs are visible, rest are invisible. Thes...

How to do inclusive range queries when only half-open range is supported (ala SortedMap.subMap)

On SortedMap.subMap This is the API for SortedMap<K,V>.subMap: SortedMap<K,V> subMap(K fromKey, K toKey) : Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive. This inclusive lower bound, exclusive upper bound combo ("half-open range") is something that is prevalent in Java, and...

Comparable and Comparator contract with regards to null

Comparable contract specifies that e.compareTo(null) must throw NullPointerException. From the API: Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false. On the other hand, Comparator API mentions nothing about what needs to happen when c...

Generics compiles and runs in Eclipse, but doesn't compile in javac

Note: This is a spin-off from Comparable and Comparator contract with regards to null This code compiles and runs fine in Eclipse (20090920-1017) import java.util.*; public class SortNull { static <T extends Comparable<? super T>> Comparator<T> nullComparableComparator() { return new Comparator<T>() { @Override...

C#: IEnumerable, GetEnumerator, a simple, simple example please!

Hi there, Trying to create an uebersimple class that implements get enumerator, but failing madly due to lack of simple / non-functioning examples out there. All I want to do is create a wrapper around a data structure (in this case a list, but I might need a dictionary later) and add some functions. public class Album { public rea...

How to get a Class literal from a generically specific Class

There are methods like these which require Class literals as argument. Collection<EmpInfo> emps = SomeSqlUtil.select( EmpInfo.class, "select * from emps"); or GWT.create(Razmataz.class); The problem presents itself when I need to supply generic specific classes like EmpInfo<String> Razmataz<Integer> The following wou...

Java Webservice with generic methods

Hi, I was wondering if it is possible to make a generic webservice method in java like this: @WebMethod public <T extends Foo> void testGeneric(T data){ However when I try to consume this with a Java client I get an error stating: [ERROR] Schema descriptor {http://####/}testGeneric in message part "parameters" is not defined and co...

How can I make this simple C# generics factory work?

I have this design: public interface IFactory<T> { T Create(); T CreateWithSensibleDefaults(); } public class AppleFactory : IFactory<Apple> { ... } public class BananaFactory : IFactory<Banana> { ... } // ... The fictitious Apple and Banana here do not necessarily share any common types (other than object, of course). I don't w...

How do I put all the types matching a particular C# interface in an IDictionary?

I have a number of classes all in the same interface, all in the same assembly, all conforming to the same generic interface: public class AppleFactory : IFactory<Apple> { ... } public class BananaFactory : IFactory<Banana> { ... } // ... It's safe to assume that if we have an IFactory<T> for a particular T that it's the only one of t...

How to convert value of Generic Type Argument to a concrete type?

I am trying to convert the value of the generic type parameter T value into integer after making sure that T is in fact integer: public class Test { void DoSomething<T>(T value) { var type = typeof(T); if (type == typeof(int)) { int x = (int)value; // Error 167 Cannot convert type 'T' to 'int'...

How do I access static variables in an enum class without a class instance?

I have some code that processes fixed length data records. I've defined the record structures using java enums. I've boiled it down the the simplest example possible to illustrate the hoops that I currently have to jump through to get access to a static variable inside the enum. Is there a better way to get at this variable that I'm o...