generics

How to handle comparison operator for Generic Java class accepting String or Integer

♀尐吖头ヾ 提交于 2021-02-07 07:52:56
问题 I am interested in using a generic Java class which can handle String or Integer data type. I'd like to utilize the comparison operator, but it appears to have a compile error: Operator < cannot be used on generic T How can I achieve using comparison operators such as <, >, =, etc. for T ? (Assuming T is Number or String ). public class Node<T> { public T value; public Node(){ } public void testCompare(T anotherValue) { if(value == anotherValue) System.out.println("equal"); else if(value <

How to sort list type generic if more than one property?

时光总嘲笑我的痴心妄想 提交于 2021-02-07 06:49:15
问题 I have a list-generic that has a property (class type). I need a sort method for Z parameters (TrainingSet): public override List<TrainingSet> CalculatedDistancesArray (List<TrainigSet> ts, double x, double y, int k) { for (int i =0; i < ts.Count; i++) { ts[i].Z = (Math.Sqrt(Math.Pow((ts[i].X - x), 2) + Math.Pow((ts[i].Y - y), 2))); } // I want to sort according to Z ts.Sort(); //Failed to compare two elements in the array. List<TrainingSet> sortedlist = new List<TrainingSet>(); for (int i =

Scala copy case class with generic type

假如想象 提交于 2021-02-07 05:27:05
问题 I have two classes PixelObject , ImageRefObject and some more, but here are just these two classes to simplify things. They all are subclasses of a trait Object that contains an uid. I need universal method which will copy a case class instance with a given new uid . The reason I need it because my task is to create a class ObjectRepository which will save instance of any subclass of Object and return it with new uid . My attempt: trait Object { val uid: Option[String] } trait UidBuilder[A <:

What is a List`1 object?

蓝咒 提交于 2021-02-07 04:24:12
问题 A System.IO.FileInfo has a Target member. Using Get-Item -Path * -Include 't.txt' | Get-Member shows it to have a Target member that is a CodeProperty . Using GetType() shows it to be a List`1 C:>Get-Item -Path * -Include 't.txt' | ForEach-Object { $_.Target.GetType() } IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True List`1 System.Object C:>Get-Item -Path * -Include 't.txt' | % { $_.Target.GetType() | % { $_.FullName } } System.Collections.Generic.List`1[[System

Inject an Interface with Angular 4

夙愿已清 提交于 2021-02-07 04:24:12
问题 I'm trying to create a generic DeleteableConfirmationComponent that will allow me to show a confirmation dialog and invoke the delete method from any injected service implementing a Deleteable infterface. To do so, I've created this Interface: export interface Deleteable { delete(object); } and I have a service that implements it: @Injectable() export class LocalityService implements Deleteable { delete(locality): Observable<Locality> { // Delete logic. } } For the

Generic constructors and reflection

喜夏-厌秋 提交于 2021-02-07 01:57:44
问题 Is it possible to see which constructor was the generic one? internal class Foo<T> { public Foo( T value ) {} public Foo( string value ) {} } var constructors = typeof( Foo<string> ).GetConstructors(); The property 'ContainsGenericParameters' returns me for both constructors false. Is there any way to find out that constructors[0] is the generic one? They both have the same signature, but I would like to call the "real" string one. EDIT: I want to invoke the given type using ilGen.Emit(

Generic constructors and reflection

笑着哭i 提交于 2021-02-07 01:41:44
问题 Is it possible to see which constructor was the generic one? internal class Foo<T> { public Foo( T value ) {} public Foo( string value ) {} } var constructors = typeof( Foo<string> ).GetConstructors(); The property 'ContainsGenericParameters' returns me for both constructors false. Is there any way to find out that constructors[0] is the generic one? They both have the same signature, but I would like to call the "real" string one. EDIT: I want to invoke the given type using ilGen.Emit(

Generic constructors and reflection

一曲冷凌霜 提交于 2021-02-07 01:39:28
问题 Is it possible to see which constructor was the generic one? internal class Foo<T> { public Foo( T value ) {} public Foo( string value ) {} } var constructors = typeof( Foo<string> ).GetConstructors(); The property 'ContainsGenericParameters' returns me for both constructors false. Is there any way to find out that constructors[0] is the generic one? They both have the same signature, but I would like to call the "real" string one. EDIT: I want to invoke the given type using ilGen.Emit(

Problems with recursive generic type in c#

这一生的挚爱 提交于 2021-02-06 15:24:10
问题 I've got some C# code that compiles fine under both mono and the Microsoft's .net compilers, but only runs on mono. The error message is (newlines added by me) Unhandled Exception: System.TypeLoadException: Could not load type 'Hasse.Groups.Heavy.Product.PowerGroup`1' from assembly 'Hasse, Version=1.0.x.y, Culture=neutral, PublicKeyToken=null' because it has recursive generic definition. The type actually has a recursive generic definition, so my question is: why does it work with mono? [The

Problems with recursive generic type in c#

纵饮孤独 提交于 2021-02-06 15:21:10
问题 I've got some C# code that compiles fine under both mono and the Microsoft's .net compilers, but only runs on mono. The error message is (newlines added by me) Unhandled Exception: System.TypeLoadException: Could not load type 'Hasse.Groups.Heavy.Product.PowerGroup`1' from assembly 'Hasse, Version=1.0.x.y, Culture=neutral, PublicKeyToken=null' because it has recursive generic definition. The type actually has a recursive generic definition, so my question is: why does it work with mono? [The