generics

Java Generics: Overriding generic methods, wildcard shorthand?

青春壹個敷衍的年華 提交于 2020-02-03 09:32:22
问题 If I have an interface with a generic method like the following: public interface Thing { void <T extends Comparable<? super T>> doSomething(List<T> objects); } I need that ugly generic typespec in some places, but most implementations don't actually need it: public class ICareAboutSortingThing implements Thing { @Override public void <T extends Comparable<? super T>> doSomething(List<T> objects) { ... } } public class IDontCareAboutSortingThingx100 implements Thing { @Override public void <T

Java Generics: Overriding generic methods, wildcard shorthand?

蹲街弑〆低调 提交于 2020-02-03 09:32:18
问题 If I have an interface with a generic method like the following: public interface Thing { void <T extends Comparable<? super T>> doSomething(List<T> objects); } I need that ugly generic typespec in some places, but most implementations don't actually need it: public class ICareAboutSortingThing implements Thing { @Override public void <T extends Comparable<? super T>> doSomething(List<T> objects) { ... } } public class IDontCareAboutSortingThingx100 implements Thing { @Override public void <T

Java Generics: Overriding generic methods, wildcard shorthand?

无人久伴 提交于 2020-02-03 09:31:18
问题 If I have an interface with a generic method like the following: public interface Thing { void <T extends Comparable<? super T>> doSomething(List<T> objects); } I need that ugly generic typespec in some places, but most implementations don't actually need it: public class ICareAboutSortingThing implements Thing { @Override public void <T extends Comparable<? super T>> doSomething(List<T> objects) { ... } } public class IDontCareAboutSortingThingx100 implements Thing { @Override public void <T

C# generic does *not* implement something

折月煮酒 提交于 2020-02-03 08:04:26
问题 I know I can make a method like private T MyFun<T>() where T : IMyInterface {...} Can I do the reverse, i.e. where T does not implement IMyInterface? The specific use case is that I don't want to allow nullables, but I'm curious just in general. 回答1: No, in the general case you cannot specify an "exclusion list". However, to prevent Nullable types from being allowed, you can use the "where T : class" constraint. Because Nullable is a struct, that will have the desired effect. Edit: Oops, it

How can I create my own “parameterized” type in Python (like `Optional[T]`)?

不羁的心 提交于 2020-02-03 08:00:26
问题 I want to create my own parameterized type in Python for use in type hinting: class MaybeWrapped: # magic goes here T = TypeVar('T') assert MaybeWrapped[T] == Union[T, Tuple[T]] Never mind the contrived example; how can I implement this? I looked at the source for Union and Optional, but it looks like some fairly low-level hackery that I'd like to avoid. The only suggestion in the documentation comes from an example re-implementation of Mapping[KT,VT] that inherits from Generic. But that

IList using covariance and contravariance in c#, is this possible?

依然范特西╮ 提交于 2020-02-03 05:17:45
问题 would this be possible? (I don't have vs. 2010, so I can't try it myself, sorry) public interface IComplexList<out TOutput, in TInput> where TOutput : TInput { public IEnumerator<TOutput> GetEnumerator(); public void Add(TInput item); } public interface IList<T> : IComplexList<T, T> { } If I get it right, you could use this to actually implement covariance and contravariance in the same interface. 回答1: No, you can't. In your example IList<T> is invariant. IList<T> would require to declare in

Pass int array in where clause of LINQ Query

痴心易碎 提交于 2020-02-03 05:15:10
问题 There is a class Public class Student { int id; int rollNum; String Name; } a method Public List<Student> GetAllStudents() { var studentList = GetMeAll(); // Simple select * query to get all students //unfortunately i can't make changes in this method so have to write a linq query to //filter data. //getting int array ids correctly from some method var filterList = FilterStudentsByID(ids,StudentList); } I want to write a method that takes an int array as input and returns a List Public List

Invalid Cast of Type Constrained C# Generic

*爱你&永不变心* 提交于 2020-02-03 04:59:52
问题 Targeting .net 4.0, I'm trying to build the following classes: public class ConfigurationElementCollection<TElement, TParent> where TElement : ParentedConfigElement<TParent>, new() where TParent : class { public TParent ParentElement { get; set; } protected ConfigurationElement CreateNewElement() { //************************************************** //COMPILER GIVES TYPE CONVERSION ERROR ON THIS LINE! //************************************************** return new TElement { ParentCollection

Interfaces with Generics - Setting to NIL

大城市里の小女人 提交于 2020-02-03 03:15:15
问题 I am trying to implement clear in the following example code in Delphi 2009. interface ... TFoo<T : IInterface> = class(TObject) FField : T; procedure Clear; end; ... implementation ... procedure TFoo<T>.Clear; begin // Line Below Results In // E2010 Incompatible types: 'T' and 'Pointer' FField := nil; end; ... I could understand the complie time error if "T" was not constrained. But since "T" must be an Interface, I would have thought this syntax would have worked. Is there away to set

Interfaces with Generics - Setting to NIL

浪子不回头ぞ 提交于 2020-02-03 03:15:06
问题 I am trying to implement clear in the following example code in Delphi 2009. interface ... TFoo<T : IInterface> = class(TObject) FField : T; procedure Clear; end; ... implementation ... procedure TFoo<T>.Clear; begin // Line Below Results In // E2010 Incompatible types: 'T' and 'Pointer' FField := nil; end; ... I could understand the complie time error if "T" was not constrained. But since "T" must be an Interface, I would have thought this syntax would have worked. Is there away to set