generics

Generics: What's a “CONSTRUCTOR constraint”?

霸气de小男生 提交于 2020-01-11 01:40:27
问题 I made a custom TObjectList descendant designed to hold subclasses of a base object class. It looks something like this: interface TMyDataList<T: TBaseDatafile> = class(TObjectList<TBaseDatafile>) public constructor Create; procedure upload(db: TDataSet); end; implementation constructor TMyDataList<T>.Create; begin inherited Create(true); self.Add(T.Create); end; I want each new list to start out with one blank object in it. It's pretty simple, right? But the compiler doesn't like it. It says

Is there Boxing/Unboxing when casting a struct into a generic interface? [duplicate]

依然范特西╮ 提交于 2020-01-10 21:50:07
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Structs, Interfaces and Boxing From the MSDN: http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. But what about generic interfaces? For example, int derives from both IComparable and IComparable<int> . Let's say I have the following code: void foo(IComparable value) { /* etc. */ } void

Is there Boxing/Unboxing when casting a struct into a generic interface? [duplicate]

£可爱£侵袭症+ 提交于 2020-01-10 21:48:47
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Structs, Interfaces and Boxing From the MSDN: http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. But what about generic interfaces? For example, int derives from both IComparable and IComparable<int> . Let's say I have the following code: void foo(IComparable value) { /* etc. */ } void

Typesafe forName class loading

时光总嘲笑我的痴心妄想 提交于 2020-01-10 19:30:53
问题 How do I call Class.forName() when the result is a generic type? Usually I can use asSubclass() , but here the only way I see to do it is a cast, which kindof sticks out & bugs me when everything else is nicely typed with generics. The scenario goes something like this: There is a .jar with one entry point main class that has a main() . It takes an option of a classname (and some others, irrelevant here). The class given implements Callable<Integer> . This class is loaded, inited & launched.

Casting to Generic base class failing

≡放荡痞女 提交于 2020-01-10 05:11:08
问题 I am failing to see why my attempt to cast to a generic base class is not working. The basic structure of the code is as follows. interface ICmd { } class Context { } class Cmd<TContext> : ICmd where TContext : Context { } class MyContext : Context { } class MyCmd : Cmd<MyContext> { } So now I have an instance of ICmd and want to cast it to Cmd as follows var base = cmd as Cmd<Context> base is always null after this line is executed. changing cast to be specific for the context only and it

About error using Java generics: “type parameter S is not within its bound”

丶灬走出姿态 提交于 2020-01-10 04:54:11
问题 I am writing some classes using Generics but I can't find a solution for the class SolutionsSubset and so I a getting the error "type parameter S is not within its bound". I have read previous questions about the same error but I can't solve it for my case. Could anybody help me to improve my knowledge about generics? Any reference to a good book (I can find in google a lot of information but if someone can reccommend a book, tutorial, etc. will be welcome). Although I tried to keep in mind

Capturing wildcards in Java generics

╄→尐↘猪︶ㄣ 提交于 2020-01-10 03:52:09
问题 From this Oracle Java tutorial: The WildcardError example produces a capture error when compiled: public class WildcardError { void foo(List<?> i) { i.set(0, i.get(0)); } } After this error demonstration, they fix the problem by using a helper method: public class WildcardFixed { void foo(List<?> i) { fooHelper(i); } // Helper method created so that the wildcard can be captured // through type inference. private <T> void fooHelper(List<T> l) { l.set(0, l.get(0)); } } First, they say that the

NInject with Generic interface

泄露秘密 提交于 2020-01-09 12:18:10
问题 I have defined one interface and one class: public interface IRepository<T> { } public class RoleRepository:IRepository<Domain_RoleInfo> { } Inject here: public RoleService { [Inject] public RoleService(IRepository<Domain_RoleInfo> rep) { _roleRep=rep; } } How can I perform Dependency Injection With Ninject,say how to bind? I have written a helper class as below, it works fine with non-generic interface.but how to refactor it support generic interface as above? public class

Is it possible to restrict a Swift generic class function return type to the same class or subclass?

随声附和 提交于 2020-01-09 10:52:49
问题 I am extending a base class (one which I do not control) in Swift. I want to provide a class function for creating an instance typed to a subclass. A generic function is required. However, an implementation like the one below does not return the expected subclass type. class Calculator { func showKind() { println("regular") } } class ScientificCalculator: Calculator { let model: String = "HP-15C" override func showKind() { println("scientific") } } extension Calculator { class func create<T

Is it possible to restrict a Swift generic class function return type to the same class or subclass?

痞子三分冷 提交于 2020-01-09 10:51:10
问题 I am extending a base class (one which I do not control) in Swift. I want to provide a class function for creating an instance typed to a subclass. A generic function is required. However, an implementation like the one below does not return the expected subclass type. class Calculator { func showKind() { println("regular") } } class ScientificCalculator: Calculator { let model: String = "HP-15C" override func showKind() { println("scientific") } } extension Calculator { class func create<T