trait

A few notes about using Scala traits as mixins (construction order, overridden methods) alvin September 7, 2017 - 7:00am

Here are a few notes about using Scala traits as mixins, specifically:

- The order in which mixed-in traits are constructed
- The order in which overridden methods in traits are called when multiple traits are mixed in

Scala best practices (idioms) (from the Scala Cookbook)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is the introduction to Chapter 20, _Idioms_ (Scala best practices).

When I first came to Scala from Java, I was happy with the small things, including eliminating a lot of ;, (), and {} characters, and writing more concise, Ruby-like code. These were nice little wins that made for “a better Java.”

How to wrap Scala traits so they can be used from Java code

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 17.7, “How to write Scala traits so they can be used from Java code.”

Problem

You’ve written a Scala trait with implemented methods and need to be able to use those methods from a Java application.

Solution

You can’t use the implemented methods of a Scala trait from Java, so wrap the trait in a class.

How to use serialization in Scala (Serializable trait)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.8, “How to use serialization in Scala (Serializable trait).”

Problem

You want to serialize a Scala class and save it as a file, or send it across a network.

Solution

The general approach is the same as Java, but the syntax to make a class serializable is different.

To make a Scala class serializable, extend the Serializable trait and add the @SerialVersionUID annotation to the class:

How to extend a Java interface like a Scala trait

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a very short recipe, Recipe 8.9, “How to extend a Java interface like a Scala trait.”

Problem

You want to implement a Java interface in a Scala application.

Solution

In your Scala application, use the extends and with keywords to implement your Java interfaces, just as though they were Scala traits.

Given these three Java interfaces:

How to dynamically add a Scala trait to an object instance

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a very short recipe, Recipe 8.8, “How to dynamically add a Scala trait to an object instance.”

Problem

Rather than add a trait to an entire class, you just want to add a trait to an object instance when the object is created.

Solution

Add the trait to the object when you construct it. This is demonstrated in a simple example:

How to declare that a Scala trait can only be mixed into a type that has a specific method

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 8.7, “How to declare that a Scala trait can only be mixed into a type that has a specific method.”

Problem

You only want to allow a trait to be mixed into a type (class, abstract class, or trait) that has a method with a given signature.

Solution

Use a variation of the self-type syntax that lets you declare that any class that attempts to mix in the trait must implement the method you specify.

How to define a Scala trait so it can only be subclassed by a certain type

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 8.6, “How to mark a Scala trait so it can only be subclassed by a certain type.”

Problem

You want to mark your trait so it can only be used by types that extend a given base type.

Solution

To make sure a trait named MyTrait can only be mixed into a class that is a subclass of a type named BaseType, begin your trait with a this: BaseType => declaration, as shown here: