generics

How to return the instance of first-class module's nested type from a function?

百般思念 提交于 2020-06-16 18:36:16
问题 Context: I am trying to implement something like OOP observable pattern in OCaml with using first-class modules. I have a project with a list of modules and want to extend them with observation without changing. To minimize code duplication I created Subject module and plan to use it as a part of the common way (in the project context) for this extending. I declared three module types: OBSERVER: module type OBSERVER = sig type event type t val send : event -> t -> t end OBSERVABLE: module

How to return the instance of first-class module's nested type from a function?

不羁的心 提交于 2020-06-16 18:36:07
问题 Context: I am trying to implement something like OOP observable pattern in OCaml with using first-class modules. I have a project with a list of modules and want to extend them with observation without changing. To minimize code duplication I created Subject module and plan to use it as a part of the common way (in the project context) for this extending. I declared three module types: OBSERVER: module type OBSERVER = sig type event type t val send : event -> t -> t end OBSERVABLE: module

Generic parameter 'T' could not be inferred

a 夏天 提交于 2020-06-15 06:51:49
问题 I'm refactoring my code and adding support for Swift generics . I'm stuck with a compiler error. My code is: func dequeueReusableViewController<T: UIViewController where T: Reusable>() -> T { // Try to fetch view controller from the reuse queue. if !self.viewControllerReuseQueue.isEmpty { return self.viewControllerReuseQueue.popFirst()! as! T } // Ask delegate to instantiate a new view controller. return delegate!.reusableViewControllerForPageViewController(self) } This compiles smoothly.

Generic parameter 'T' could not be inferred

强颜欢笑 提交于 2020-06-15 06:51:49
问题 I'm refactoring my code and adding support for Swift generics . I'm stuck with a compiler error. My code is: func dequeueReusableViewController<T: UIViewController where T: Reusable>() -> T { // Try to fetch view controller from the reuse queue. if !self.viewControllerReuseQueue.isEmpty { return self.viewControllerReuseQueue.popFirst()! as! T } // Ask delegate to instantiate a new view controller. return delegate!.reusableViewControllerForPageViewController(self) } This compiles smoothly.

Generic parameter 'T' could not be inferred

老子叫甜甜 提交于 2020-06-15 06:51:12
问题 I'm refactoring my code and adding support for Swift generics . I'm stuck with a compiler error. My code is: func dequeueReusableViewController<T: UIViewController where T: Reusable>() -> T { // Try to fetch view controller from the reuse queue. if !self.viewControllerReuseQueue.isEmpty { return self.viewControllerReuseQueue.popFirst()! as! T } // Ask delegate to instantiate a new view controller. return delegate!.reusableViewControllerForPageViewController(self) } This compiles smoothly.

How to create a generic typesafe HashMap by class type? [duplicate]

a 夏天 提交于 2020-06-14 04:18:45
问题 This question already has answers here : Java map with values limited by key's type parameter (5 answers) Closed 6 years ago . I'd like to create a HashMap that maps specific class types to one single specific new object. Later I want to pass the class type and get the reference to that specific object. Simple example: Map<Class<?>, ?> values = new HashMap<>(); public <T> t get(Class<T> type) { return values.get(type); } //pet and car do not share any interface or parent class class Pet;

Type bound in Scala function complicates piping to method reference

元气小坏坏 提交于 2020-06-12 08:46:11
问题 I've implemented a simple event service that allows for the publishing of read-only events ( Event ), and cancellable events ( PreEvent ). Cancellable events are reduced by all handlers and the result is returned to the caller. The Event interaction works as expected, but the PreEvent type bound of T <: PreEvent is giving me some issues: (1) When matching a PreEvent its copy has to be explicitly cast to T for the compiler to realise it is the same type as the method parameter. (2) When

Type bound in Scala function complicates piping to method reference

你说的曾经没有我的故事 提交于 2020-06-12 08:42:30
问题 I've implemented a simple event service that allows for the publishing of read-only events ( Event ), and cancellable events ( PreEvent ). Cancellable events are reduced by all handlers and the result is returned to the caller. The Event interaction works as expected, but the PreEvent type bound of T <: PreEvent is giving me some issues: (1) When matching a PreEvent its copy has to be explicitly cast to T for the compiler to realise it is the same type as the method parameter. (2) When

Type bound in Scala function complicates piping to method reference

二次信任 提交于 2020-06-12 08:42:17
问题 I've implemented a simple event service that allows for the publishing of read-only events ( Event ), and cancellable events ( PreEvent ). Cancellable events are reduced by all handlers and the result is returned to the caller. The Event interaction works as expected, but the PreEvent type bound of T <: PreEvent is giving me some issues: (1) When matching a PreEvent its copy has to be explicitly cast to T for the compiler to realise it is the same type as the method parameter. (2) When

Method signature selection for lambda expression with multiple matching target types

别说谁变了你拦得住时间么 提交于 2020-06-10 07:51:45
问题 I was answering a question and ran into a scenario I can't explain. Consider this code: interface ConsumerOne<T> { void accept(T a); } interface CustomIterable<T> extends Iterable<T> { void forEach(ConsumerOne<? super T> c); //overload } class A { private static CustomIterable<A> iterable; private static List<A> aList; public static void main(String[] args) { iterable.forEach(a -> aList.add(a)); //ambiguous iterable.forEach(aList::add); //ambiguous iterable.forEach((A a) -> aList.add(a)); /