generics

Generic Multiplication of Different Types

Hello, In simple words, I want to implement a function like that: let myfunction (scalar : float) (quantities : ''T list) = List.fold (fun acc quantity -> acc + quantity * scalar) LanguagePrimitives.GenericZero<_> quantities : 'T Note that I am multiplying a value of generic type 'T and a float value (quantit...

Why this simple java generic function wont compile?

While f1 does compile, the very similar f2 wont I and just cant explain why. (Tested on Intellij 9 and Eclipse 3.6) And really I thought I was done with that kind of question. import java.util.*; public class Demo { public List<? extends Set<Integer>> f1(){ final List<HashSet<Integer>> list = null; return list; ...

Shorthand declaration of long generic collection types.

I have looked at a lot of example c# generic code and remember seeing a syntactic declaration trick that created an alternative shorthand type for a long generic dictionary type. Mixing C# and C++ it was something like: typedef MyIndex as Dictionary< MyKey, MyClass>; This then allowed the following usage: class Foo { MyIndex _cla...

C# Derived Type to Generic List as Parameter

Hello :) I am trying to do the following in C# 4.0: I have a Base Class and 2 derived classes public class Base {} public class DerivedClass1 : Base {} public class DerivedClass2 : Base {} I want to do something like this, but it doesn't work. How to I tell a Generic List to accept a Base Class and the derived classes of the bas...

What does new() mean in this context

Hi! What does new() mean in the following context: public interface ISpecification<TEntity> where TEntity : class,new() ...

Create generic class with internal constructor

Is it possible to construct an object with its internal constructor within a generic method? public abstract class FooBase { } public class Foo : FooBase { internal Foo() { } } public static class FooFactory { public static TFooResult CreateFoo<TFooResult>() where TFooResult : FooBase, new() { return new TFooResult(...

How to impose, in a C# generic type parameter, that some operators are supported?

Hello, I am recently working on a C# class library implementing an algorithm. The point is that I would like the users of the library to be able to choose the machine precision (single or double) the algorithm should operate with, and I'm trying to do it with generics. So, for instance: Algorithm<double> a = new Algorithm<doubl...

Casting generic class types to non-generic or calling methods of their instances?

I am trying to do one of the following: Dummy d = (Class<Dummy>)super.getEntity(); // incompatible Types - cannot cast Dummy d = (Dummy)super.getEntity(); // incompatible Types - cannot cast d.foo(); or Class<Dummy> d = (Class<Dummy>)super.getEntity(); // works d.foo(); // method cannot be found Dummy d is defined in an abstract ...

multiple nested wildcard - arguments not applicable

I've heavily simplified my problem. Here's how it reads. I'm trying to figure out why the following code does not compile: List<AnonType<AnonType<?>>> l = new ArrayList<AnonType<AnonType<?>>>(); l.add( new AnonType<AnonType<String>>() ); where public class AnonType<T> { T a; List<T> b; } The compiler error is saying that ad...

C#: Convert T to T[]

I would like to convert T to T[] if it is an array. static T GenericFunction<T>(T t) { if (t == null) return default(T); if (t.GetType().IsArray) { //if object is an array it should be handled //by an array method return (T) GenericArrayFunction((T[])t); } ... } s...

Has .NET 4 solved this simple generic interface problem?

Suppose you have an interface like this: public interface IDoSomething<out T> { T DoSomething(object value); } To be able to call DoSomething on that interface without knowing the type of T, you have to create this: public interface IDoSomething { object DoSomething(object value); } Modify your original interface to inherit...

(Co)Variance on Lists different then on Stacks in Scala?

Hi, When I'm writing this code, I've got a Compile error in Scala var s: Stack[_ <: Number] = new Stack[Integer]; s.push(new Integer(1)); //compile-error: type mismatch; found :Integer required: _$bqjyh where type _$bqjyh <: Number s.push(null); //compile-error: type mismatch; found : Null(null) required: _$2 where type _$2 <: Objec...

method parameter with multiple interface restrictions.

Hi, In C# it's possible to defined a method parameter with two interface restrictions. This with bounds. For example. interface IA { int A {get;} } interface IB { int B {get;} } void Foo<T>(T param1) where T: IA, IB {} So two interfaces, and the first parameter (param1) of the method Foo should implement both interfaces. But ...

Using Generics in a non-collection-like Class.

Hi, I'm working on an engine which is meant to be configurable by the user (not end user, the library user) to use different components. For example, let's say the class Drawer must have an ITool, Pencil or Brush, and an IPattern, Plain or Mosaic. Also, let's say a Brush must have an IMaterial of either Copper or Wood Let's say the effe...

Event aggregator - cast object to interface

Hello! How to find out if object supports IHandle<T> and is there any possible workaround to achieve this in delphi (2010, XE)? Also has anybody seen a nice implementation of event aggregator for delphi? IHandle<TMessage> = interface procedure Handle(AMessage: TMessage); end; EventAggregator = class private FSubscribers: TList<TObj...

Odd error when trying to use removeChild/addChild

I have the following line in some of my haxe code: removeChild(_screens[Helpers.indexOf(_screenNames, _activeScreen)]); (_screens is a List, GameScreen is extending from Sprite, _activeScreen is a String, _screenNames is a List, and Helpers.indexOf does the obvious) However, i get the error: List<com.haxelib.GameScreen> should be Ar...

Calling indexer from within the same (generic) class

public class MyClass<T> { public T this[int index] { get { ... } set { ... } } public void MyMethod<T>() { int middleIndex = ...; T value = this[middle...

java object hierarchy, and passing objects to functions

I have created a TreeMap like so: TreeMap<Integer, ArrayList<MyClass>> wrap = new TreeMap<Integer, ArrayList<MyClass>>(); I have created a constructor like so: public foo (TreeMap<Integer, Collection<Spot> > objects) { this.field = objects; } However, eclipse gives me a red squigly when I use the constructor, with my wrap vari...

sun vs eclipse autoboxing difference

I am trying to create a static entry to a bunch of non-static util methods and getting this error with the sun compiler that I am not getting in eclipse: "type parameters of X cannot be determined; no unique maximal instance exists for type variable X with upper bounds X,java.lang.Object" public class Resource { protected <X> X from...

Adding a derived interface to generic interface

I've got these interfaces: And this code: IList<IFinder<IDomainObject>> finders = new List<IFinder<IDomainObject>>(); IFinder<IOrder> orderFinder = this.mocks.StrictMock<IFinder<IOrder>>(); finders.Add(orderFinder); I get this error on the third line: Argument '1': cannot convert from 'Mes.FrameworkTest.Repository.Finders.IFind...