polymorphism

How to use polymorphism in functional programming? [closed]

帅比萌擦擦* 提交于 2020-02-03 03:15:50
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . How to use polymorphism in functional programming (with dynamic type system)? Let's consider following example (first in OOP second in

Why does Scala fail to find a secondary implicit value in this one particular case?

假装没事ソ 提交于 2020-02-01 03:05:07
问题 I am having a hard time explaining the difference in behavior between additional implicit values sought by either a primary implicit value or an implicit conversion. Specifically, this works: trait Foo[A] implicit def fooString: Foo[String] = null implicit def value[A](implicit foo: Foo[A]) = 5 > implicitly[Int] 5 But this does not: implicit def conversion[A](x: Int)(implicit foo: Foo[A]) = new { def aMethod = 5 } > 1.aMethod could not find implicit value for parameter foo: test.Foo[A]

Understanding serialization of polymorphic objects in C++

北城以北 提交于 2020-01-30 13:19:02
问题 EDIT: I realised that the code below is a good example of what you cannot do in C++ with anything that is not a POD. There doesn't seem to exist a way to escape from having a typeid into the classes and do some sort of switch or table lookup (both of which must be carefully maintained) on the receiver side to rebuild the objects. I have wrote some toy code to serialise objects and two separate mains to write/read them to/from a file. common.h: #include <iostream> using namespace std; template

Understanding serialization of polymorphic objects in C++

女生的网名这么多〃 提交于 2020-01-30 13:18:52
问题 EDIT: I realised that the code below is a good example of what you cannot do in C++ with anything that is not a POD. There doesn't seem to exist a way to escape from having a typeid into the classes and do some sort of switch or table lookup (both of which must be carefully maintained) on the receiver side to rebuild the objects. I have wrote some toy code to serialise objects and two separate mains to write/read them to/from a file. common.h: #include <iostream> using namespace std; template

Right design pattern to deal with polymorphic collections of objects

好久不见. 提交于 2020-01-30 06:28:57
问题 Suppose I have the following classes: class BaseObject { public: virtual int getSomeCommonProperty(); }; class Object1: public BaseObject { public: virtual int getSomeCommonProperty(); // optional int getSomeSpecificProperty(); }; class BaseCollection { public: virtual void someCommonTask(); }; class Collection1: public BaseCollection { public: virtual void someCommonTask(); // optional void someSpecificTask(); }; Each collection, derived from BaseCollection, deals with a specific object type

Right design pattern to deal with polymorphic collections of objects

狂风中的少年 提交于 2020-01-30 06:27:05
问题 Suppose I have the following classes: class BaseObject { public: virtual int getSomeCommonProperty(); }; class Object1: public BaseObject { public: virtual int getSomeCommonProperty(); // optional int getSomeSpecificProperty(); }; class BaseCollection { public: virtual void someCommonTask(); }; class Collection1: public BaseCollection { public: virtual void someCommonTask(); // optional void someSpecificTask(); }; Each collection, derived from BaseCollection, deals with a specific object type

Behavior of method overloading in java [duplicate]

徘徊边缘 提交于 2020-01-30 03:27:15
问题 This question already has answers here : Two methods with the same name in java (3 answers) Closed 6 years ago . I tried the following code public class HelloWorld { public void printData(Test t) { System.out.println("Reached 1"); } public void printData(newTest t) { System.out.println("Reached 2"); } public void printData(newTest1 t) { System.out.println("Reached 3"); } public static void main(String args[]) { Test t1 = new Test(); HelloWorld h = new HelloWorld(); h.printData(t1); NewTest t2

Why does Haskell's `head` crash on an empty list (or why *doesn't* it return an empty list)? (Language philosophy)

折月煮酒 提交于 2020-01-28 13:20:25
问题 Note to other potential contributors: Please don't hesitate to use abstract or mathematical notations to make your point. If I find your answer unclear, I will ask for elucidation, but otherwise feel free to express yourself in a comfortable fashion. To be clear: I am not looking for a "safe" head , nor is the choice of head in particular exceptionally meaningful. The meat of the question follows the discussion of head and head' , which serve to provide context. I've been hacking away with

Polymorphism and derived classes in CUDA / CUDA Thrust

心不动则不痛 提交于 2020-01-27 07:56:13
问题 This is my first question on Stack Overflow, and it's quite a long question. The tl;dr version is: How do I work with a thrust::device_vector<BaseClass> if I want it to store objects of different types DerivedClass1 , DerivedClass2 , etc, simultaneously? I want to take advantage of polymorphism with CUDA Thrust. I'm compiling for an -arch=sm_30 GPU (GeForce GTX 670). Let us take a look at the following problem: Suppose there are 80 families in town. 60 of them are married couples, 20 of them

Polymorphism and derived classes in CUDA / CUDA Thrust

有些话、适合烂在心里 提交于 2020-01-27 07:56:05
问题 This is my first question on Stack Overflow, and it's quite a long question. The tl;dr version is: How do I work with a thrust::device_vector<BaseClass> if I want it to store objects of different types DerivedClass1 , DerivedClass2 , etc, simultaneously? I want to take advantage of polymorphism with CUDA Thrust. I'm compiling for an -arch=sm_30 GPU (GeForce GTX 670). Let us take a look at the following problem: Suppose there are 80 families in town. 60 of them are married couples, 20 of them