generics

Can a Delphi generic class descend from its class argument?

I've been trying to define a generic, inheritable TSingleton class. Here's what I had in progress: TSingleton<RealClass, InheritsFrom : class> = class(InheritsFrom) strict private class var FInstance : RealClass; protected procedure InstanceInitialization;virtual; public destructor Destroy; override; class proced...

Generic method with generic type problem

I have problem with constraints on generic method. Here is code for all classes: namespace Sdk.BusinessObjects { public interface IBusinessObject { } } namespace Sdk.BusinessObjects { [DataContract] public class AccountDetails : IBusinessObject { [DataMember] public virtual Guid AccountId { get; ...

Why can a generic list be casted to its type parameter

Why does the following code compile? Why is it allowed to cast a generic list to its type parameter if the parameter is an interface but not a super-interface of the generic? What does this mean? //Connection can be substituted by any interface List<Connection> list = null; Connection c = (Connection) list; ...

Java: generic interfaces and functions in such

I am still experimenting with how Java handles generics. I stumbled upon the fact/issue/thing that if you have a generic interface like A<T>, you cannot really check afterwards if some object is actually implementing A<B> or A<C>. I wondered if that could cause actual problems. Now I have tried this code: static interface A<T> { void ...

Are there any plans for the JVM to support generics at runtime?

You know, like the CLR does. Is anyone even admitting the lack of runtime generic information is a problem, and working to solve it? ...

Implementing Java generic interface in Clojure

I'm trying to get the hang of clojure in a selenium2/webdriver project using the webdriver-clj wrapper for webdriver. However, since the webinterface is heavily scripted, I need to have an option to wait until certain elements are created by the script, instead of on page load. So I was trying to create a wait-for function in clojure, ...

Generically checking for null that won't box nullables on a non-constrained type.

Let say I have the following method:: public static int CountNonNullMembers<T>(this IEnumerable<T> enumerable) { if (enumerable == null) throw new ArgumentNullException("enumerable"); int count = 0; foreach (var x in enumerable) { if (x != null) count++; } return count; } And I have these 3 arrays:: v...

Java: Should I favor generics when implementing various tree structures?

I've just came over this article, that suggest various techniques with generics. Author decided to use following: public class BinarySearchTree<T extends Comparable<? super T>> { And I don't get it. Why did author decide to use private Entry<T> root; and not just private Comparable root ? What particular advantage can generic tre...

Linq-to-object slowness. Now I'm just downright confused.

Okay, to build on my previous question: http://stackoverflow.com/questions/3999761/generically-checking-for-null-that-wont-box-nullables-on-a-non-constrained-type One user suggested putting a constraint for class and for struct and I also implemented the UnboxT pattern of specializing for the three types and storing that logic in delega...

With Java, is it acceptable to use an unbounded wildcard type as a method's argument, and then check and cast it to a parameterized type?

If I use unbounded wildcard types for two collections (each collection will have a different type) as the arguments for a method: private void doAssertion(List<?> testList, List<?> generatedList) Inside this method, can I first check the type of objects in these collections, and then cast the collection to a parameterized type? This j...

Casting to List(Of T) in VB.NET

In C# it's possible to cast to List<T> - so if you have: List<Activity> _Activities; List<T> _list; The following will work: _list = _Activities as List<T>; but the translated line with VB.NET which is: _list = TryCast(_Activities, List(Of T)) throws a compilation error. So I've had a good hunt around and experimented with LINQ ...

How to compare 2 objects of generic type

Is there a way to compare two objects that are generic? I'm supposed to find the largest object in a linked list. My first guess was to use the Object's class compareTo method, but I couldn't get that to work. Thanks ...

Why do I get "Illegal generic type for instanceof"?

Given: public class C<T> { private class D { public boolean equals( Object o ) { if ( !(o instanceof D) ) // line 4 return false; D other = (D)o; // line 6 return i == other.i; } int i; } } I get: C.java:4: illegal generic type for inst...

Is creating a C# generic method that accepts (nullable) value type and reference type possible?

I want to create a simple method that accepts both value type and reference type parameters, i.e. int is value, and string is reference. So this is what I start with: public bool areBothNotNull<T>(T? p1, T? p2) { return (p1.HasValue && p2.HasValue); } So I want to be able to use it like this: var r1 = areBothNotNull<int>(3, 4);...

Partial type inference

I have a generic method like this (simplified version): public static TResult PartialInference<T, TResult>(Func<T, TResult> action, object param) { return action((T)param); } In the above, param is of type object on purpose. This is part of the requirement. When I fill in the types, I can call it like this: var test1 = PartialIn...

How to merge two seperate - yet similar - codebases into one SVN rep?

Hi guys, I have /var/www/cool_codebase on www.example.com AND I have /var/www/cool_codebase on www.example.net The codebases are for the same web app running on different servers. There is some specialisation between the codebases (client-specific bits and bobs etc) - but not too much. One codebase has files that the other doesn't an...

How to copy a subset of a list using generics and reflection

I need to copy a sub set of items from one list to another. However I do not know what kind of items are in the list - or even if the object being passed is a list. I can see if the object is a list by the following code t = DataSource.GetType(); if (t.IsGenericType) { Type elementType = t.GetGenericArguments()[0]; } What I cann...

Java passing List<String> as Class<T>, wants to know the type of the list

Hi, I have a question about generics. I have a function that knows about a class type of object that I want to create: public static <T> XmlParserInterface<T> createXmlParser(Class<T> rootType, String currentTagName) { XmlParserInterface<T> result = null; if (rootType.equals(List.class)) { result = new XmlParserList<T>(); } // ...

Would templates (or other technology) support the following construct?

This is kind of a follow-up from my other question. When I first heard about generics, it was before the release of Delphi 2009 (Where they introduced it first). I know it was supported in .Net before that, but I have yet to dig in that realm. Reading about generics, I learned that it allowed class to have a variable argument to it, an...

Delphi Generics > Dictionary with default value

Hi all, I would like to have a dictionary that returns a default value when the search key is not found. Reading from the documentation: Generics.Collections.Tdictionary […] This class provides a mapping […] and initial content. 1 - How? Is there a way to do it ala Python: {1: ‘one’; 2:’two’} ? Generics.Collections.TDictionary.TryGet...