howto distinguish composition and self-typing use-cases

Posted by ayvango on Stack Overflow See other posts from Stack Overflow or by ayvango
Published on 2012-06-16T01:39:58Z Indexed on 2012/06/19 3:16 UTC
Read the original article Hit count: 288

Filed under:
|
|

Scala has two instruments for expressing object composition: original self-type concept and well known trivial composition. I'm curios what situations I should use which in.

There are obvious differences in their applicability. Self-type requires you to use traits. Object composition allows you to change extensions on run-time with var declaration.

Leaving technical details behind I can figure two indicators to help with classification of use cases. If some object used as combinator for a complex structure such as tree or just have several similar typed parts (1 car to 4 wheels relation) than it should use composition. There is extreme opposite use case. Lets assume one trait become too big to clearly observe it and it got split. It is quite natural that you should use self-types for this case.

That rules are not absolute. You may do extra work to convert code between this techniques. e.g. you may replace 4 wheels composition with self-typing over Product4. You may use Cake[T <: MyType] {part : MyType} instead of Cake { this : MyType => } for cake pattern dependencies. But both cases seem counterintuitive and give you extra work.

There are plenty of boundary use cases although. One-to-one relations is very hard to decide with. Is there any simple rule to decide what kind of technique is preferable?

self-type makes you classes abstract, composition makes your code verbose. self-type gives your problems with blending namespaces and also gives you extra typing for free (you got not just a cocktail of two elements but gasoline-motor oil cocktail known as a petrol bomb).

How can I choose between them? What hints are there?

Update:

Let us discuss the following example:

Adapter pattern. What benefits it has with both selt-typing and composition approaches?

© Stack Overflow or respective owner

Related posts about scala

Related posts about composition