polymorphism

F# - Understanding types that use generics

半腔热情 提交于 2020-06-23 16:05:48
问题 I am having an F# exam in 10 days and as I am currently doing old exam sets, I ran into a problem understanding generics and especially types that have two polymorphic arguments. The questions should be rather easy to solve, but how it works syntactically, I am not sure. The old exam question is as follows: The following type Sum<'a,'b> comprises two different kinds of values type Sum<'a,'b> = | Left of 'a | Right of 'b Now I need to write two values of type Sum<int list, bool option> , one

Turning a non-pure virtual function into pure in a subclass

耗尽温柔 提交于 2020-06-14 06:27:10
问题 So, I have this polymorphic hierarchy: ClassA Is not abstract, no pure virtual functions, but a few virtual functions ClassB:public ClassA Defines an extended interface for a certain type of subclass; is abstract with pure virtual functions ClassC:public ClassB Usable class, no more subclassing Here's the deal, I will have objects of ClassA and ClassC thrown together into containers and iterated through. To perform this iteration, a non-pure virtual function is present in ClassA but is empty

How to combine F-bounded polymorphism with associated types in Scala?

牧云@^-^@ 提交于 2020-06-01 05:49:11
问题 I have a trait called Graphlike for things that work as a graph. Notably, one of the properties I want to have is that the method g.subgraph(Set(1, 2, 3)) would return a subgraph of the same type with just the vertices 1, 2 and 3. Apparently, this means that I want F-bounded polymorphism and that Graphlike looks something like this: trait Graphlike[A <: Graphlike[A]] { type Vertex def subgraph(selectedVertices: Set[Vertex]): A } I also have a trait representing an automaton with associated

Two polymorphic classes in one function

99封情书 提交于 2020-05-29 07:03:31
问题 I have this code with State monads: import Control.Monad.State data ModelData = ModelData String data ClientData = ClientData String act :: String -> State ClientData a -> State ModelData a act _ action = do let (result, _) = runState action $ ClientData "" return result addServer :: String -> State ClientData () addServer _ = return () scenario1 :: State ModelData () scenario1 = do act "Alice" $ addServer "https://example.com" I am trying to generalise it with polymorphic type-classes

Two polymorphic classes in one function

走远了吗. 提交于 2020-05-29 07:03:17
问题 I have this code with State monads: import Control.Monad.State data ModelData = ModelData String data ClientData = ClientData String act :: String -> State ClientData a -> State ModelData a act _ action = do let (result, _) = runState action $ ClientData "" return result addServer :: String -> State ClientData () addServer _ = return () scenario1 :: State ModelData () scenario1 = do act "Alice" $ addServer "https://example.com" I am trying to generalise it with polymorphic type-classes

Two polymorphic classes in one function

谁说我不能喝 提交于 2020-05-29 07:02:13
问题 I have this code with State monads: import Control.Monad.State data ModelData = ModelData String data ClientData = ClientData String act :: String -> State ClientData a -> State ModelData a act _ action = do let (result, _) = runState action $ ClientData "" return result addServer :: String -> State ClientData () addServer _ = return () scenario1 :: State ModelData () scenario1 = do act "Alice" $ addServer "https://example.com" I am trying to generalise it with polymorphic type-classes

Polymorphism and SwiftUI

谁说胖子不能爱 提交于 2020-05-23 21:40:26
问题 Given the following example: class ProfileTab: Identifiable { let id = UUID() let name: String init(name: String) { self.name = name } } class ProfileQuoteTab: ProfileTab { let answer: String let prompt: String init(name: String, answer: String, prompt: String) { self.answer = answer self.prompt = prompt super.init(name: name) } } class ProfileImageTab: ProfileTab { let image: Image init(name: String, image: Image) { self.image = image super.init(name: name) } } struct ContentView: View { let

having difficulties to make a vector of polymorphic objects using shared_ptr and unique_ptr in C++ Visual studio 2019 Cmake project

吃可爱长大的小学妹 提交于 2020-05-16 03:54:08
问题 For an exercise i need to build a vector of polymorphic objects and for some reason both shared and Unique ptr make linkage errors 2019 and 1120 when i use them. i have no option to use the old way of memory allocation with New and Delete so i have to make it work. i tried various of different syntax options for doing this and still with no luck. *note: we use Cmake to bind the project together in visual studio and also we splitting the objects into header and cpp files. here are my objects

polymorphism: calling overrided functions without pointers

点点圈 提交于 2020-05-14 08:49:38
问题 I am doing some experimentation with C++. I've been imporessioned by some behaviours with polymorphism. In other languages (such as c#), when I assign an object based on a derived class to an object of BaseType: this object starts working with the derived class code. Or If I have a list of BaseType objects and I put derived class based objects in it: every element works according to the specific Type. In c++ no... I obtained this behaiviour in C++ just using pointers. Is there an alternative

How to avoid downcasting in this specific class hierarchy design?

让人想犯罪 __ 提交于 2020-05-13 14:09:57
问题 I've got an assignment to create a sort of a multi-platform C++ GUI library. It wraps different GUI frameworks on different platforms. The library itself provides an interface via which the user communicates uniformly regardless of the platform he's using. I need to design this interface and underlying communication with the framework properly. What I've tried is: Pimpl idiom - this solution was chosen at first because of its advantages - binary compatibility, cutting the dependency tree to