generics

How to store delegates in a List

How can I store delegates (named, anonymous, lambda) in a generic list? Basically I am trying to build a delegate dictionary from where I can access a stored delegate using a key and execute it and return the value on demand. Is it possible to do in C# 4? Any idea to accomplish it? Note : Heterogeneous list is preferable where I can stor...

Wildcard capture error

The classes I am dealing with are as follows, public interface One{ public Two get(); } public class Foo{...} public class Bar extends Foo{...} public abstract class Parent<T extends Foo> implements One {...} public abstract class Child<T extends Foo> extends Parent<T> I have a static method that will return a Child object giv...

Java Generics 'Incompatible Type' Compile-Time Error

For a CS class I am writing a linked list implementation of a linked list interface created by my professor. The assignment requires us to use generics for the list. What I have created, I think, is pretty standard. public class MyLinkedList<T> implements ADTListInterface { ... private class Node<T> { Node<T> head; ...

How do I use StructureMap with generic unclosed types using Scan with a "greedy" constructor

Between various Stack Overflow questions and blog posts there is a pretty reasonable amount of documentation on the topic of open generics and StructureMap. Unfortunately, I must be missing something as my attempts at using scan to perform the configuration along with a class implementation that has a "greedy" constructor have yet work. ...

Shortcut for "null if object is null, or object.member if object is not null"

I'm trying to write a generic extension method that let's me do this: this.startDate = startDateXAttribute.NullOrPropertyOf<DateTime>(() => { return DateTime.Parse(startDateXAttribute.Value); }); NullOrPropertyOf() would return null if it's used on a null object (e.g. if startDateXAttribute was null), or return the result of a Fun...

Unable to create property for a generic class

public class Address { public string streetno; public string streetname; public string suburb; public string postcode; public Country country; } public class Country { public string name; } public class Person<A> where A : new() { public A address; public Person() { address.country = new ...

Have a class implement an unspecified generic interface in java

Hi, I have the following "generic" question Here is a my generic message interface. public interface Message<P,R> { void setParams(P Params); // and the corresponding getter void setResults(R results); } For simplicity reasons I would like to specify a number (of interfaces) of messages by predefining the paramet...

(How) Can I do this with Java Generics?

I'd like to to something like the following in Java and don't really know what to search for: public interface Subject<T> { } public interface EMailAddress extends Subject<String> { } public interface Validator<T extends Subject<V>> { Set<InputErrors> validate(final V thing); // this does not compile } I basically would like the p...

implicit operator on generic types

Is there anything wrong with using an implicit operator like the following: //linqpad c# program example void Main() { var testObject = new MyClass<int>() { Value = 1 }; var add = 10 + testObject; //implicit conversion to int here add.Dump(); // 11 } class MyClass<T> { public T Value { get; set; } public static imp...

Create object of parameter type

hey. Is it possible to have a method that allows the user to pass in a parameter of a certain type and have the method instantiate a new object of that type? I would like to do something like this: (I don't know if generics is the way to go, but gave it a shot) public void LoadData<T>(T, string id, string value) where T : new() ...

Any substitute solutions of generic type attributes?

Becase we can't use generic type attributes, are there any substitute solutions? Maybe an example is helpful to discuss: public abstract class ErrorHandler { } public class AccessHandler : ErrorHandler { } public class ConnectionHandler : ErrorHandler { } public class OtherHandler : ErrorHandler { } public class CoHandler<T> : Attribut...

How to ensure that type parameters are different in Scala?

With the following definition it's possible to ensure the concrete type parameters are equal: trait WithEqual[T1 >: T2 <: T2, T2] So the line type A = WithEqual[Int, Int] will be legal. Now my question is: How to achieve exactly the opposite? Thus, the following line should not compile: type B = WithUnequal[Int, Int] ...

Method signatures in classes which extend a generic class

If I have a generic class like this: public class Repository<T> { public string Greeting(T t) { return "Hi, I'm " + t.ToString(); } } which is extended like this: public class FooRepository : Repository<Foo> If FooRepository has a method called Greeting(Foo foo), does that method have the same signature as the base class ...

C# generics problem

hi, I have a method (see below) public T ExecuteScalar<T>(string sSql, params SqlParameter[] parameters) { if (!string.IsNullOrEmpty(sSql)) { DataAccess dal = new DataAccess(ConnectionString); DebugOutput_SQL(sSql, parameters); object value = null; value = dal.Exe...

How to deduce class type from method type in C++ templates?

In templates as shown below, I would like the call Run(&Base::foo) succeed without the need to name the Base type twice (as is done in the compiling Run<Base>(&Base::foo) call). Can I have that? Possibly without adding a ton of Boost headers? With the provided code, I get an error of: prog.cpp:26: error: no matching function for call t...

Java generics and wildcards: How to make this code compile?

I'm writing some matchers using the Hamcrest 1.2 library, but I'm having a hard time with Java wildcards. When I try to compile the following code public class GenericsTest { public void doesNotCompile() { Container<String> container = new Container<String>(); // this is the desired assertion syntax assertT...

Java: would it make sense to have the throw-attribute generic?

When you pass a callback in some form to another function, you often have to fullfil some interface to be able to pass such callback. That callback interface will often restrict you in what type of exceptions you can throw. The most natural way for me would be that the called function would automatically rethrow (or ignore) the exceptio...

C# call Generic method dynamically

Given the following Interfaces: interface IEntity { int Id{get;} } interface IPerson : IEntity { string Name{get;} int Age{get;} } interface ITeacher : IPerson { string StaffId{get;} } interface IStudent : IPerson { string StudentId{get;} string Courses{get;} } interface IRepository { T Get<T>(int id)...

Cannot compile a class which implements an interface without type parameter

I have the following test code: public interface Container<I> { public void addClass(Class<?> clazz); } public class MyContainer implements Container { public void addClass(Class<?> clazz) {} } and I get the following error when trying to compile these two class: MyContainer.java:1: MyContainer is not abstract and does no...

(Re)using generic parameters of a constraining type without declaring them

It probably isn't possible, but I want to check if something like this can be expressed in a simple way: // obviously doesn't work class Foo<T> : IFoo<T1,T2> where T: Bar<T1,T2> { // ... Baz<T1,T2> getBaz(){...} } Right now I declare Foo<T1,T2>, but I don't like it semantically, and the only way to get the constraint is to...