generics

avoiding cast to Nothing in generic method

scala> def foo[U](t: Any) = t.asInstanceOf[U] foo: [U](t: Any)U scala> val s: String = foo("hi") scala> val n = foo("hi") java.lang.ClassCastException: java.lang.String cannot be cast to scala.runtime.Nothing$ at .<init>(<console>:6) at .<clinit>(<console>) at RequestResult$.<init>(<console>:9) at RequestResult$.<clinit...

Strange decompiled code from event subscribe code in a generic class

This simple class public class Test<T> { public static void A(Window wa, Window wb) { wa.Closed += (s, e) => wb.Close(); } } Gets compiled to this (I'm using Reflector to decompile) : public class Test<T> { [CompilerGenerated] private sealed class <>c__DisplayClass1 { public Window wb; ...

Best way to maintain references to a collection of generic types.

Say I have this interface: public interface IRepository<T> where T : Entity { T Get(string query); void Save(T entity); } And I have a couple of concrete classes like (where User and Project, of course, inherit from Entity): public class UserRepository : IRepository<User> { ... } public class ProjectRepository : IProjectRepositor...

Java template question

I have instantiated an object like this: GraphMatrixDirected<String, Integer> g g is passed to a function like this: floyd(g); floyd's signature looks like this: public void floyd(GraphMatrixDirected<V,E> g) Eclipse gives me an error saying: The method floyd(GraphMatrixDirected<V,E>) in the type GraphMatrix<V,E> is not applic...

How do I reflect back the name of a Type supplied as a generic?

How to I reflect back the name of a type that is supplied as a generic parameter? I would really like to know how this is done in both C# and VB.NET. See the following example code and expected response. In C#: public void Test<T>() { Console.WriteLine("The supplied type is {0}",???) } In VB.NET Public Sub Test(Of T)() ...

Ensure that objects implement Comparable

I have a litte problem and was wondering how to solve it. I have a generic class Tuple<A,B> and now I would like to sort their tuples according to A and B. It should look like this: Unsorted: (1,5) (2,8) (6,8) (1,4) (2,4) Sorted: (1,4) (1,5) (2,4) (2,8) (6,8) For that reason I thought of implementing a generic compare method (pu...

How would one go about dealing with generic enums in Java?

So, I have an abstract class like: public abstract class AbstractParent <E extends Enum<E>> {...} Somewhere in a non-abstract method inside AbstractParent, I would like to iterate over the values of E. Is this possible? For a better example: public abstract class AbstractParent <E extends Enum<E>> { ... protected void doSome...

How to access a method on an object that's passed in as a parameter to a Generic Function in C#

Hi, I have a generic method that has some parameter of a generic Type. What I want to do, is be able to access the method on this generic type parameter inside my function. public void dispatchEvent<T>(T handler, EventArgs evt) { T temp = handler; // make a copy to be more thread-safe if (temp != nul...

Why can't you cast a constrained open generic typed to the constrained type?

I think I must be missing something, why can't I compile this: class Foo<T> where T : Bar { T Bar; } abstract class Bar { } class MyBar : Bar { } static void Main(string[] args) { var fooMyBar = new Foo<MyBar>(); AddMoreFoos(fooMyBar); } static void AddMoreFoos<T>(Foo<T> FooToAdd) where T : Bar { var listOfFoos = new...

Using Where to specify different generics.

I'm writing a bijective dictionary class, but I want to ensure the two generic types are not the same type for two reasons. Firstly, I would like it to implement the IDictionary interface in both directions, but public class BijectiveDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IDictionary<TValue, TKey> gives me " 'Bije...

How can I use LINQ to see if a list<string> contains a string that starts with 'blah'?

I have a List with a few strings in it. I want to see if it contains a a string that starts with 'blah' however, I'm not sure how to use the (this IEnumerable source, value):bool overload of List.Contains. ...

Structure Map 2.6.1

Hi, In my current project I'm currently trying to replace the Windsor IoC in favour of structure map (2.6.1). But having a bit of problem registering some generic types. How would I register IFilterConverter<T> to use FilterConverter<SomeSpecificType>. I've tried ConnectImplementationsToTypesClosing(IFilterConverter) but from what I've ...

Generic class with method taking a different generic class

Sorry for asking what seems like such an obvious question. I have an "adapter" generic class of type T where T is a defined interface. I want to create a method in that class which takes a different type of generic adapter. public interface ICommon { } public class TypeOne : ICommon { } public class TypeTwo : ICommon { } public class ...

Need help in Event Handling

I have a delegate delegate string Mathop<T,F>(T a,F b); and I am declaring an event like event Mathop<T,F> someevent; But here I am getting an error. It says 'T' could not be found. I want my Mathop delegate to work as an eventhandler for my event. What I am doing wrong here. ...

Why doesn't Java allow "new List<T>"?

To create a List, why doesn't Java allow them to be created then elements added one by one? This works: public static List<TrackedItem> create(List<Item> items) { TrackedItem[] arr = new TrackedItem[items.size()]; int i = 0; for (Item item : items) { arr[i] = TrackedItem.createOrUpdate(item); i++; ...

How do I convert these generic method calls from C# to Java

I have the following C# helper class which retrieves a Json String from a remote server and casts it to the type I need. public class JsonHelper { private JavaScriptSerializer serializer = new JavaScriptSerializer(); public T GetValue<T>(string methodName, object param) where T : IConvertible { string result = GetJs...

[C#] Casting object to generic T

Hello. I'm having a little trouble with some casts that i cant figure out out to solve. I have this interface and the following implementations: public interface IConfigureServiceOnServer { void Configure(Service service); } public sealed class ServiceConfigurator : IConfigureServiceOnServer { ... } I'm loading these types at ...

signature of Object.getClass() method

The class Object contains the following method: public final Class<? extends Object> getClass(). why the return type of this method is Class<? extends Object> ...

Java generics infering type

hi, my IDE generates a warning when i use the following code: aMethod(List.class); "Type safety: The expression of type List needs unchecked conversion to conform to ..." Sadly when i try to let the IDE fix it, it doesn't work. What is the proper syntax to infere the generic type ? Thx in advance for any help. edit: Signature of t...

Instance of type generic

The my question is this: Why can not instantiate a generic type with new T () and instead with newInstance() of the class Class you can do? ...