polymorphism

Idiomatic way of returning multiple types in Rust [duplicate]

霸气de小男生 提交于 2021-02-04 08:40:48
问题 This question already has an answer here : Multiple return types from a method (1 answer) Closed 4 months ago . I'm writing a program which parses a number of files. These files can fall into a number of different categories and I don't know which in advance. I makes sense to create a type for each type of data the files can contain, but I'm struggling with getting this datatype out of my parser and into my model/main program. In most other programming languages I would probably define a

How to do generic polymorphism on open types in C#?

 ̄綄美尐妖づ 提交于 2021-01-31 05:39:55
问题 I have an interface like: interface IFoo<in T> { ... } And I want to have a collection of those classes WITHOUT forcing a specific generic parameter for all collection elements, like: class IFooCollection { List<IFoo<?>> _items; public IFooCollection(List<IFoo<?>> items) { _items = items; } ... } 回答1: Since you don't want to force a specific type, make an abstract class or an interface this way: interface IFoo { } And inherit it in your type-specific Foo (with generic parameters), like:

How to do generic polymorphism on open types in C#?

。_饼干妹妹 提交于 2021-01-31 05:38:22
问题 I have an interface like: interface IFoo<in T> { ... } And I want to have a collection of those classes WITHOUT forcing a specific generic parameter for all collection elements, like: class IFooCollection { List<IFoo<?>> _items; public IFooCollection(List<IFoo<?>> items) { _items = items; } ... } 回答1: Since you don't want to force a specific type, make an abstract class or an interface this way: interface IFoo { } And inherit it in your type-specific Foo (with generic parameters), like:

Making double and std::vector<double> covariant

强颜欢笑 提交于 2021-01-29 22:32:45
问题 I am trying to make a wrapper for "any" data type such that they have common interface called IValue so it will be possible to call get() on any concrete Value and return the value of concrete data type. In simplest case I just want to be able to call get() on double and std::vector<double> . In my understanding these data types need to be covariant (which it is not at all in my code). The following is my raw imagination of the code: //template<typename T> class IValue { protected: // typedef

Making double and std::vector<double> covariant

泄露秘密 提交于 2021-01-29 22:31:04
问题 I am trying to make a wrapper for "any" data type such that they have common interface called IValue so it will be possible to call get() on any concrete Value and return the value of concrete data type. In simplest case I just want to be able to call get() on double and std::vector<double> . In my understanding these data types need to be covariant (which it is not at all in my code). The following is my raw imagination of the code: //template<typename T> class IValue { protected: // typedef

Making double and std::vector<double> covariant

独自空忆成欢 提交于 2021-01-29 21:51:30
问题 I am trying to make a wrapper for "any" data type such that they have common interface called IValue so it will be possible to call get() on any concrete Value and return the value of concrete data type. In simplest case I just want to be able to call get() on double and std::vector<double> . In my understanding these data types need to be covariant (which it is not at all in my code). The following is my raw imagination of the code: //template<typename T> class IValue { protected: // typedef

Base Class reference to Pointer of Derived Class

大憨熊 提交于 2021-01-29 20:53:14
问题 I would like to have, in a base class A, a reference (or pointer if not possible) to a pointer in a derived class. This would look like this : BaseClassController { public : //Constructors, destructor,... protected : BaseDataClass *& m_current; } DerivedClassAController : public BaseClassController { public : //Constructors, destructor,... protected : DerivedDataClassA * m_currentA; } DerivedClassBController : public BaseClassController { public : //Constructors, destructor,... protected :

Polymorphism - object and type

有些话、适合烂在心里 提交于 2021-01-29 19:02:25
问题 I'm a bit confused on the concept of 'Subclass referenced as Superclass' within polymorphism. (referencing here: https://stackify.com/oop-concept-polymorphism/) Lets say we have the superclass animal and the subclass dog, where dog extends animal. The following work: animal testSuper = new animal(); dog testDog = new dog(); animal testSuperDog = new dog(); Could anyone explain a bit further on whats happening behind the scenes for #3? When we do 'new dog()', are we creating an object of the

Implementation of methods of interface

空扰寡人 提交于 2021-01-29 07:17:31
问题 I want to implement a representation of matrices. for that I have two types of matrices - regular and sparse, which differ in their implementation - one holds a vector, and the second a map of indices and value, both inherit from Matrix class. For that, I'm using the strategy pattern, where I create the base abstract class Matrix , two classes that inherit from Matrix - RegMatrix and SparseMatrix , and MyMatrix that holds a pointer to a Matrix . I want to implement the + operator, which

Dynamic Dispatching in Ada with Access Types

江枫思渺然 提交于 2021-01-29 03:52:22
问题 I am trying to create a package that has Dynamic dispatching using access types. I have achieved Dynamic Dispatching using Class Types using This Q/A as a guide. I keep getting a compilation error that says: cannot call abstract subprogram. This makes me think that the compiler either doesnt recognize the specialized subprogram, or doesnt recognize the type as a specialized type. But both seem right to me... I dont get it. main.2.ada with Ada.Text_IO; with Animal.Cat; procedure Main is Tabby