generics

Properties with generic type

梦想的初衷 提交于 2020-02-23 07:04:13
问题 Is it possible to have properties with generic type? What I am trying to do: Have a base class called Value with the following structure: class Value { var genericProperty: T init<T>(type: T) { switch type.self { case is Int.Type: genericProperty is Int case is [Int.Type]: genericProperty is [Int] default: genericProperty is Any } } } Then have a bunch of subclasses that define what the type of genericProperty should be. Something like this: class IntValue: Value { override init<T>(type: T) {

Using a generic type of any nested subclass within its abstract superclass

人盡茶涼 提交于 2020-02-23 07:04:10
问题 Suppose you have the following abstract java class: public abstract class AbstractRequestHandler<I,O> { I input; O output; } and the following child classes hierarchy: public abstract class AbstractUserRequestHandler<I extends User,O> extends AbstractRequestHandler<I,O>{...} public abstract class AbstractUniversityRequestHandler<I extends UniversityUser> extends AbstractUserRequestHandler<I,String>{...} public class StudentRequestHandler extends AbstractUniversityRequestHandler<Student>{...}

Gwt Request Factory. Generics and Inheritance on client side

时光总嘲笑我的痴心妄想 提交于 2020-02-23 05:49:15
问题 I am trying to write a generic class to avoid code repetition. I would like to have generic methods for: Get Entity/Model from server by id. Get List of all Entities/Models from server. Send to server and save in db Entity/Model. It should work with Generic classes, e.g.: Services<PizzaProxy> factory = GWT.create(Services.class); factory.initialize(new SimpleEventBus()); GenericContext<PizzaProxy> context = factory.genericContext(); context.get().to(new Receiver<List<GenericProxy<PizzaProxy>>

Gwt Request Factory. Generics and Inheritance on client side

六眼飞鱼酱① 提交于 2020-02-23 05:46:48
问题 I am trying to write a generic class to avoid code repetition. I would like to have generic methods for: Get Entity/Model from server by id. Get List of all Entities/Models from server. Send to server and save in db Entity/Model. It should work with Generic classes, e.g.: Services<PizzaProxy> factory = GWT.create(Services.class); factory.initialize(new SimpleEventBus()); GenericContext<PizzaProxy> context = factory.genericContext(); context.get().to(new Receiver<List<GenericProxy<PizzaProxy>>

Java generic method erasure and inheritance

天大地大妈咪最大 提交于 2020-02-23 04:24:32
问题 I am running into a problem with javas generics and overriding methods. Imagine I have a deep tree-like class hierarchy. The top-level class defines a method foo which takes 1 argument of type Strategy. Strategy has a generic type parameter. Each class in my class hierarchy needs to override foo to limit the kind of Strategy it can be passed so that the generic type parameter of the strategy matches the declaring class. Below is an example: abstract static class TopLevelClass { static final

Custom equality of types with statically resolved type parameters

笑着哭i 提交于 2020-02-21 23:57:33
问题 How to implement custom equality methods in F# types with statically resolved type parameters? I've tried doing it this way: [<CustomEqualityAttribute>] type Fraction< ^a when ^a: equality and ^a : (static member (*): ^a * ^a -> ^a) > (nominator: ^a, denominator: ^a) = member inline this.Nominator = nominator member inline this.Denominator = denominator member inline this.IsEqualTo(other: Fraction< ^a >) = this.Nominator * other.Denominator = other.Nominator * this.Denominator override inline

How to put an interface constraint on a generic method in C# 3.5?

雨燕双飞 提交于 2020-02-21 12:37:30
问题 I want to achieve something like this in C# 3.5: public void Register<T>() : where T : interface {} I can do it with class or struct, but how to do it with an interface? 回答1: C# and the CLR don't support overall interface constraints, although you can constrain it to a particular interface (see other answers). The closest you can get is 'class' and check the type using reflection at runtime I'm afraid. Why would you want an interface constraint in the first place? 回答2: If you are asking about

Array of protocol type

若如初见. 提交于 2020-02-19 10:17:53
问题 I have checked all answers about this problem on stackoverflow, but still can not figure out how to fix this. My model looks like this protocol Commandable: Equatable { var condition: Condition? {get set} func execute() -> SKAction } And 3 structs which implement this protocol struct MoveCommand: Commandable { var movingVector: CGVector! //MARK: - Commandable var condition: Condition? func execute() -> SKAction { ... } } extension MoveCommand { // MARK:- Equatable static func ==(lhs:

Array of protocol type

你说的曾经没有我的故事 提交于 2020-02-19 10:16:43
问题 I have checked all answers about this problem on stackoverflow, but still can not figure out how to fix this. My model looks like this protocol Commandable: Equatable { var condition: Condition? {get set} func execute() -> SKAction } And 3 structs which implement this protocol struct MoveCommand: Commandable { var movingVector: CGVector! //MARK: - Commandable var condition: Condition? func execute() -> SKAction { ... } } extension MoveCommand { // MARK:- Equatable static func ==(lhs:

Why do I have to specify variable name in TS generic return type?

我怕爱的太早我们不能终老 提交于 2020-02-16 06:50:51
问题 I am using typescript to write some function and this is the one that TS accepts: export const useSomething = <T>() => { const useStorage = <T>(key: string, initialData: T) : [T, (newData: T) => Promise<void>] => { const [data, setState] = useState<T>(initialData); const setData = async(newData: T) : Promise<void> => { await storage.setItem<T>(key, newData); }; return [data, setData]; } }; But inithially I wanted to write the return type of useStorage in this way: [T, (T) => Promise<void>]