generics

Error: Generic Array Creation

I don't understand the error of Generic Array Creation. First I tried the following: public PCB[] getAll() { PCB[] res = new PCB[list.size()]; for (int i = 0; i < res.length; i++) { res[i] = list.get(i); } list.clear(); return res; } Then I tried doing this: PCB[] res = ne...

Tuple.Create return type

Given void foo(Tuple<object> t) { } void bar() { foo(Tuple.Create("hello")); } the c# compiler returns error CS1502: The best overloaded method match for 'foo(System.Tuple<object>)' has some invalid arguments error CS1503: Argument 1: cannot convert from 'System.Tuple<string>' to 'System.Tuple<object>' Adding explicit types to...

Polymorphic delegates

C# chokes on delegate void Bar<T>(T t); void foo(Bar bar) { bar.Invoke("hello"); bar.Invoke(42); } The workaround is to use an interface interface Bar { void Invoke<T>(T t); } but now I need to go out of my way to define the implementations of the interface. Can I achieve the same thing with delegates and simple metho...

How do I fix this Java Generics wildcard/abstract ambiguity problem?

The code below is a simplified version of the pattern my project is using. The standard pattern we use is to have a Writer for each object type. For the subtypes of one abstract type (in this example Animal), I'd like an enum to serve as a lookup for the correct writer. abstract class Writer<T> { abstract void write(T value); } a...

Dual enumerators, second one via interface- type collisions on implementation

I have a generic class that implements IList public class ListBase<T>: IList<T>, IListBase{ IEnumerator<T> GetEnumerator(){ .... System.Collections.IEnumerator GetEnumerator(){ return GetEnumerator();} } The IListBase is an interface I use to access methods on this class for cases where I don't know the type of T at runtime. ...

How to return a Vector of inheriting classes?

Hello, I'm looking for a solution to instantiate and return a Vector (or sth comparable) of classes. My attempt looks like this (Heritage extends SuperClass): public Vector<? extends SuperClass> getAssignableClasses() { Vector<? extends SuperClass> out = new Vector<SuperClass>(); out.add(Heritage.class); //does NOT work, IDE shows erro...

How do you restrict a generic method to classes with a specified attribute?

I want to create a generic method that is only applicable to classes that have the Serializable attribute, e.g. public static int Foo<T>(T obj) where T : Serializable { ... } but obviously the above doesn't compile. And I'm guessing if I put SerializableAttribute there, it'll insist that T is an attribute, not a class with that att...

Arithmetic operations with Java Generics

I am trying to create a generic class in Java that will perform operations on numbers. In the following example, addition, as follows: public class Example <T extends Number> { public T add(T a, T b){ return a + b; } } Forgive my naivety as I am relatively new to Java Generics. This code fails to compile with the err...

Generics - Type mismatch when providing a concrete type to a generic class

Hi, Simple Java generics question: I have two classes - one of which uses generics to define its type, the other which extends this class providing a concrete type. public class Box<Item> { ... } public class Toolbox extends Box<Tool>{ ... } Given that Toolbox extends Box providing a Tool as the actual type for the generic ...

How do I remove the warning from a EasyMock.anyObject(List.class) call

Compiler can't stop complaining with this call : EasyMock.anyObject(List.class) I tried to specify list's type EasyMock.anyObject(List<MyType>.class) but it doesn't seems to be an option (anyway, it is stupid since java will erase the type during the compilation) Is there a clean way (@SuppressWarning is not a clean way IMO) to ...

Generic string to enum conversion

Hi Suppose enum: public enum SysLogsAppTypes { None, MonitorService, MonitorTool }; and here is a function to convert from the ToString() representation back to enum: private SysLogsAppTypes Str2SysLogsAppTypes(string str) { try { SysLogsAppTypes res = (SysLogsAppTypes)Enum ...

Book recommendation for C# + Generics + Lambda Expression

Can somebody recommend me book for C# 4.0 which covers Generics and Lambda Expression (scary things) I seen this but I am not sure which one covers Generics and Lambda in depth. Thanks, ...

How do statically-typed languages deal without generics?

I'm curious which statically-typed languages have no generics support (and to a lesser extent which languages historically did not have generics), and how they deal with it. Do users just cast all over the place? Is there some special sauce for basic collections, like lists and dictionaries, that allow those types to be generic? Why ...

Why *should* we use EventHandler

I hate EventHandler. I hate that I have to cast the sender if I want to do anything with it. I hate that I have to make a new class inheriting from EventArgs to use EventHandler<T>. I've always been told that EventHandler is the tradition and blah, blah...whatever. But I can't find a reason why this dogma is still around. Is there a re...

ClassCastException in Java foreach loop

In what circumstances can ClassCastException occur in the code below: import java.util.Arrays; import java.util.List; public class Generics { static List getObjects() { return Arrays.asList(1, 2, 3); } public static void main(String[] args) { List<String> list = getObjects(); for (Object o : list) ...

How to Cast to Generic Parameter in C#?

I'm trying to write a generic method for fetching an XElement value in a strongly-typed fashion. Here's what I have: public static class XElementExtensions { public static XElement GetElement(this XElement xElement, string elementName) { // Calls xElement.Element(elementName) and returns that xElement (with some validati...

Implementing interface with generic type

I'm trying to implement Spring's RowMapper interface, however, my IDE is prompting me to cast the return object to "T" and I don't understand why. Can anyone explain what I'm missing? public class UserMapper<T> implements RowMapper<T> { public T mapRow(ResultSet rs, int row) throws SQLException { User user = new User(); ...

Implementing IComparable<> in a sealed struct for comparison in a generic function

I have a C# .net 2.0 CF application where I'm validating the return value from an object. I cannot modify this structure or the function that returns it. /// This structure is returned by a function from the object under test public struct SomeType { int a; int b; char c; string d; public static SomeType Empty { get...

Scala recursive generics: Parent[Child] and Child[Parent]

Update: Clarified and expanded, since the original question was simplified too far I need a pair of traits, each of which refers to the other such that parent and child classes must relate to each other. trait Parent [C <: Child] { def foo(c: C) } trait Child [P <: Parent] { def parent: P = ... def bar = parent.foo(this) } Suc...

C# 4.0 Generics and ASP.net MVC

It appears that in C# 4.0, variance specifiers are only applicable to interface types. So let's say I have ViewModel / EditModel classes and a simple hierarchy of models. class MyEditModel<T> where T : Base { ... } class Derived1 : Base { ... } class Derived2 : Base { ... } I have a partial view that accepts a MyEditModel of any type...