composition

What is delegation in julia?

蓝咒 提交于 2021-02-08 11:11:17
问题 I see occational references to delegation, or the delegation design pattern in Julia. What is this? E.g. I see it mentioned in This file in DataStructures.jl 回答1: This is a form of polymorphism via composition (rather than inheritance) Say one has a wrapper type, wrapping some instance of a concrete subtype of AbstractT where the wrapper itself is intended to be a subtype of AbstractT (Not nesc always true, but in general). To add all the methods one exacts such a subtype of AbstractT to have

Expressive and composable error types

拈花ヽ惹草 提交于 2021-02-06 00:49:03
问题 I am struggling with the best way to report errors in a set of functions that should compose nicely, in a library I'm working on. Concretely, I have functions that look like: foo, bar, baz :: a -> Maybe a where foo can fail in only one way (a good fit for Maybe ), but bar and baz can fail in two different ways each (good fits for Either BarErrors and Either BazErrors ). One solution is to create: data AllTheErrors = TheFooError | BarOutOfBeer | BarBurnedDown | ... and make all the functions

Expressive and composable error types

做~自己de王妃 提交于 2021-02-06 00:48:52
问题 I am struggling with the best way to report errors in a set of functions that should compose nicely, in a library I'm working on. Concretely, I have functions that look like: foo, bar, baz :: a -> Maybe a where foo can fail in only one way (a good fit for Maybe ), but bar and baz can fail in two different ways each (good fits for Either BarErrors and Either BazErrors ). One solution is to create: data AllTheErrors = TheFooError | BarOutOfBeer | BarBurnedDown | ... and make all the functions

Is ArrayList<X> an aggregation or composition?

邮差的信 提交于 2021-02-05 08:35:06
问题 I am preparing for my programming exam and came across this question, I know that in aggregation the object is borrowed, and in composition the object is owned. Is the answer composition? Is an ArrayList<X> an aggregation of X or composition of X ? ArrayList<Point> pts = new ArrayList<Point>(); Point p = new Point(0., 0., 0.); pts.add(p); p.setX( 10.0 ); System.out.println(p); System.out.println(pts.get(0)); 回答1: From here: Simple rules: A "owns" B = Composition : B has no meaning or purpose

Is ArrayList<X> an aggregation or composition?

▼魔方 西西 提交于 2021-02-05 08:33:06
问题 I am preparing for my programming exam and came across this question, I know that in aggregation the object is borrowed, and in composition the object is owned. Is the answer composition? Is an ArrayList<X> an aggregation of X or composition of X ? ArrayList<Point> pts = new ArrayList<Point>(); Point p = new Point(0., 0., 0.); pts.add(p); p.setX( 10.0 ); System.out.println(p); System.out.println(pts.get(0)); 回答1: From here: Simple rules: A "owns" B = Composition : B has no meaning or purpose

How can I use internal mutability with generic type in Rust?

家住魔仙堡 提交于 2021-01-27 11:43:11
问题 I would like to design a struct in Rust that can be constructed with an object that implements the Digest trait, and abstract the behavior of the hash behind a method. Here's a simple example that doesn't compile: use digest::Digest; struct Crypto<D: Digest> { digest: D, } impl<D> Crypto<D> where D: Digest, { pub fn hash(&self, data: &[u8]) -> Vec<u8> { self.digest.chain(&data).finalize_reset().to_vec() } } This fails to compile because self is immutably borrowed in the method signature, so

Concatenative inheritance vs class inheritance in JavaScript

≡放荡痞女 提交于 2020-12-25 05:00:13
问题 concatenative inheritance works like a composition to me when I look to it at the beginning, but people keep naming it as an inheritance. classes, however, use the prototype to create a prototype chain that connects objects together. the question now is, if both concatenative inheritance and class inheritance do the same thing which one to use? here is an example of both scenarios concatenative inheritance function Person(name, address) { const _name = name const _address = address const

Implementing Composition in Java

此生再无相见时 提交于 2020-05-13 04:18:22
问题 class Book { private Chapter[] chapters = new Chapter[5]; } class Chapter { private Book book; } Is this the correct way to implement the above relationship? I need explanation on this. Thanks. 回答1: That is not enough. In Composition relationship, If whole instance is destroyed, the part instance should also be destroyed immediately . You should have some codes (some machanisms) to do that. For example if you push the instances of Chapter s from external the class (for example by using a

Implementing Composition in Java

可紊 提交于 2020-05-13 04:18:08
问题 class Book { private Chapter[] chapters = new Chapter[5]; } class Chapter { private Book book; } Is this the correct way to implement the above relationship? I need explanation on this. Thanks. 回答1: That is not enough. In Composition relationship, If whole instance is destroyed, the part instance should also be destroyed immediately . You should have some codes (some machanisms) to do that. For example if you push the instances of Chapter s from external the class (for example by using a

C++ Composition with abstract class

放肆的年华 提交于 2020-05-12 04:59:08
问题 Lets say I have an abstract class that is expensive to create and copy: class AbstractBase { public: AbstractBase() { for (int i = 0; i < 50000000; ++i) { values.push_back(i); } } virtual void doThing() = 0; private: vector<int> values; }; It has two subclasses FirstDerived : class FirstDerived : public AbstractBase { public: void doThing() { std::cout << "I did the thing in FirstDerived!\n"; } }; and SecondDerived : class SecondDerived : public AbstractBase { public: void doThing() { std: