generics

Solving generic method generic parameter type in Java like in C# ?

How to valid the type of generic parameter in Java (with out using reflection if possible) ? The target is something like C# allow to do: public <T> void doStaff() { if(T is Type1) { } if(T is Type2) { } } How to write method like that in Java ? ...

Redundant generic arguments?

I am building a entity Repository and I have an interface, IIdentifiable<T>. So entities which are identified by Guids, for example, implement IIdentifiable<Guid> with public Guid Id { get; }. So far, so good. I have an interface of IRepository<T, TIdentifier> where T : IIdentifiable<TIdentifier>. It seems to me that the TIdentifier ...

Lazy<T> implementation and .NET generics

I was looking for ways to do lazy initialization and found Lazy<T> which is included in .NET 4. I was thinking of rolling my own implementation of Lazy<T> for .NET 3.5 (with a simpler multi-thread policy), and I bumped into the following problem: Lazy has basically two types of constructors: class Lazy<T> { public Lazy(){...} // ...

How do I specify a type bound for Float and Double on a generic type in Scala?

I am writing some simple Vector and Matrix classes. They look like this: // Vector with Floats case class Vector3f(x: Float, y: Float, z: Float) { def +(v: Vector3f) = Vector3f(x + v.x, y + v.y, z + v.z) } // Vector with Doubles case class Vector3d(x: Double, y: Double, z: Double) { def +(v: Vector3d) = Vector3d(x + v.x, y + v.y, ...

Why two generic types cannot be compared with '=='?

Hi, Here is my simple code: T a; T b; if (a == b) // sth. else // sth. else When I try to compile, I get an error saying the == operator is invalid for generic types. So I have to use the object.Equals() method. Doesn't the == operator actually call the Equals method of object? Why can I use the Equals method of two generic types b...

Creating a variable that can store different instances of generic types and calling a given method on the variable, regardless of the type

I am trying to create a generic simulation runner. Each simulation implements a variety of interfaces. Eventually, it will pick up simulation types via DLLs at run time, so I will have no way of knowing the types beforehand. My Currrent Code: public class SimulationRunner<TSpace, TCell> where TSpace : I2DState<TCell> where TCe...

Passing type argument to Eiffel method

I would like to write a cast method in Eiffel which takes 'the type to cast to' as a type parameter. Is there a way to pass a type into a method in Eiffel. The only alternative I can think of is to create a new class for the conversion. Something like: class CAST [G, H] feature cast (in: LIST [G]): LIST [H] do ...

Java Generics Refactoring and Ambiguity

I have an old untyped class pre Java1.5 and need to refactor it using Java Generics to get more type security in the code. Old Code looks like this: class Foo { void setInput(Object input) {...} } Now this input could be some object but it may also be an array, nasty stuff. Making a generic version of it seems not to be trivial, I...

Is there any reason I shouldn't ignore generic types in the concrete types?

Generics in Java is noisy sometimes. The parameterized types are thrown out by the compiler anyway after compile, so my question is, is there really any drawbacks in ignoring them, as long as I declare the type of the reference to include parameterized types? e.g., Map<String, Map<Integer, String>> myMap = new HashMap<String, Map<Intege...

Was the Inclusion of Generics in Java 5.0 Worthwhile?

Aside from the added complexity, do you think that the inclusion of the Generics Mechanism in Java 5.0 was worthwhile? ...

Constructing generic lists of anonymous types and nested loops.

I wish to construct a list of anonymous types constructed by iterating through two other lists in a nested loop. var myList = new List<???>(); foreach (object x in GetAllX()) { if (Process(x)) { foreach (object y in GetAllY(x)) { myList.Add(new { X = x, Y = y ...

About Scala generics: cannot find class manifest for element type T

For a function as below: def reverse[T](a: Array[T]): Array[T] = { val b = new Array[T](a.length) for (i <- 0 until a.length) b(i) = a(a.length -i - 1) b } I am getting "error: cannot find class manifest for element type T" from line 2. Is there anyway to solve this? ...

Issue With RhinoMocks and Generics

Here's my test code: var container = MockRepository.GenerateMock<UnityContainer>(); container.Expect(e => e.RegisterType<IEventAggregator, EventAggregator>( Arg<ContainerControlledLifetimeManager>.Is.Anything)); Basically I'm going to assert that a class that takes a IUnityContainer interface sets up the container in an expected wa...

Is there a way to setup a Func with a generic object parameter?

I currently have a mapping setup to convert from one base class to another base class. I am mapping a customer class to a thrid party control. The values are similiar but there is enough differences that I can't reuse the thrid party control. _converters = new Dictionary<Type, Func<AnnotationBase, AnnotationMark>>(); _converters.Add( ty...

C#. Is is possible to have a static generic class with a base type constraint that has a method with a further base type constraint

I'd like to be able to create a static generic type with a base type constraint like public static class Manager<T> where T : HasId { public static T GetSingleById(ref List<T> items, Guid id) { // the Id is a property provided by HasId return (from i in items where i.Id == id select i).SingleOrDefault(); } } ...

C# help with Extension Method - Converting collection to another type

I want to convert from public class Party { public string Type { get; set; } public string Name { get; set; } public string Status { get; set;} } and convert to public class Contact { public string Type { get; set; } public string Name { get; set; } public string Status { get; set;...

Trouble with Generic List and add wrapper

I admit that I don't have a lot of experience with using Java Generics. Right now, I'm retrofitting some old code with Generics to reduce/simplify existing code written by a colleague who has since left the company. Basically, the system I'm working on has 6 request types: 1 General, and 5 specific that inherit from General. Each requ...

What is this? "TList does not contain a member named ..." in Delphi

I've added some of the new Generics into my Delphi 2009 program. In the Structure window of the Delphi IDE, I'm getting a bunch of errors of the form: 'TList` 1' does not contain a member named 'JumpID' at line 1031 (1031:57) My declarations and lines seem fine to me. And my program Builds without any errors and runs without problem...

Determining a Generic Type at Runtime in Non-Generic Class

I've gotten myself in a pickle, and could use the help of a guru... I have a Journal that records entries for different types: Journal(Of ParentT) - Parent could be Customer, Address, other classes The constructor of the Journal requires knowledge of the Type parameter: Public Sub New(Parent as ParentT) In my consuming form, I take ...

How to convert c# generic list to json using json.net?

I am converting my datatable to c# generic list. DataTable dt = mydata(); List<DataRow> list = dt.AsEnumerable().ToList(); Now how can i convert this list to json using json.net? Any suggestion. Sample of json format should be like this, {"Table" : [{"userid" : "1","name" : "xavyTechnologies","designation" : "", "phone" : "9999999...