Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala client composition with Traits vs implementing an abstract class

I have read that with Scala, it is generally advised to use Traits instead of Abstract classes to extend a base class.

Is the following a good design pattern and layout? Is this how Traits were intended to replace Abstract?

  • client class (with def function1)
  • trait1 class (overrides function1)
  • trait2 class (overrides function1)
  • specificClient1 extends client with trait1
  • specificClient2 extends client with trait2
like image 304
kliew Avatar asked Feb 07 '16 04:02

kliew


People also ask

What is the difference between a trait and an abstract class in Scala?

Trait supports multiple inheritance. Abstract Class supports single inheritance only. Trait can be added to an object instance. Abstract class cannot be added to an object instance.

Can Scala trait have implementation?

In Scala, we are allowed to implement the method(only abstract methods) in traits. If a trait contains method implementation, then the class which extends this trait need not implement the method which already implemented in a trait.

Why do we use traits in Scala?

Traits are used to define object types by specifying the signature of the supported methods. Scala also allows traits to be partially implemented but traits may not have constructor parameters. A trait definition looks just like a class definition except that it uses the keyword trait.


1 Answers

I don't know what your source is for the claim that you should prefer traits over abstract classes in Scala, but there are several reasons not to:

  1. Traits complicate Java compatibility. If you have a trait with a companion object, calling methods on the companion object from Java requires bizarre MyType$.MODULE$.myMethod syntax. This isn't the case for abstract classes with companion objects, which are implemented on the JVM as a single class with static and instance methods. Implementing a Scala trait with concrete methods in Java is even more unpleasant.
  2. Adding a method with an implementation to a trait breaks binary compatibility in a way that adding concrete methods to a class doesn't.
  3. Traits result in more bytecode and some additional overhead related to the use of forwarder methods.
  4. Traits are more powerful, which is bad—in general you want to use the least powerful abstraction that gets the job done. If you don't need the kind of multiple inheritance they support (and very often you don't), it's better not to have access to it.

The last reason is by far the most important in my view. At least a couple of the other issues might get fixed in future versions of Scala, but it will remain the case that defaulting to classes will constrain your programs in ways that are (at least arguably) consistent with good design. If you decide you actually really do want the power provided by traits, they'll still be there, but that'll be a decision you make, not something you just slip into.

So no, in the absence of other information, I'd suggest using an abstract class (ideally a sealed one) and two concrete classes that provide implementations.

like image 181
Travis Brown Avatar answered Sep 29 '22 16:09

Travis Brown