Self type Annotations in Cake Pattern

Self type annotations/references allows you to redefine this and is a way to declare the dependencies required by a component. Using a trait mixin, you can inject various implementations of dependencies

In layman terms, Self type annotations/references are to ensure that the class can not be instantiated without mixing in the trait explicitly specified in the notation and its members can be used in the class exactly like extending/mixing in a trait.

Syntax goes like this

trait A{
  ...some defs and vals

class B{
  this: A => of the class we can use def and vals of A too

In this example if we try to instantiate our class B it won’t

val b = new B //gives error: class B cannot be instantiated because it does not conform to its self-type B with A

It needs A to be mixed in to be instantiated

val b = new B with A

Now what’s “this: A =>”

It is the self type annotation and you can use ‘self’ or any identifier instead of ‘this’ (more details). Using A here means the class B started with mixing in trait A


The point here is the advantage that we get from using self types instead of mixing in a trait. So lets consider the following snippet that displays a class extending a trait

trait ReaderWriter{
  def read: String = "data from file"
  def write: String = "written to a file"
class Service extends ReaderWriter
  def reading = read()
  def writing = write()
val service = new Service

The problem here is that we are stuck with the ReaderWriter trait forever in case of class Service.  We can’t change the extended trait without changing our class definition


Now here we’ll use self type annotation

trait ReadWrite{
  def read: String = "data from file"
  def write: String = "data written to file"
class Service{
  this: ReaderWriter =>
  def reading: String = read()
  def writing : String = write()

We can instantiate our class Service by mixing in ReadWrite

val service = new Service with ReadWrite

Now if some trait ReadWriteDB extends ReaderWriter

trait ReadWriteDB extends ReadWrite {
  def read: String = "data from DB"
  def write: String = "data written to DB"

Then we can instantiate our service class with ReadWriteDB

val service = new Service with ReadWriteDB

Hence we can mixin child traits of explicitly specified in class definition at the time of instantiation

Using multiple traits in self reference

One more trait

trait UpdateDelete{
  def update: String = "update file data"
  def delete: String = "deleted data from file"

Now the new class definition

class Service{
  this: ReadWrite with UpdateDelete =>
  .....class definition

Class instantiation

val service = new Service with ReadWrite with UpdateDelete

So that was it for Self type annotations/references


Self type annotations/references are used in cake pattern (we haven’t discussed about it yet)

Cake pattern is one of the ways of Dependency Injection along with Constructor Pattern and Google Guice

We’ll discuss about it further in future blogs

Hope it was helpful

References : Scala in Action by Nilanjan Raychaudhuri




Written by 

Anuj Saxena is a software consultant having more than 1.5 years of experience. Anuj has worked on functional programming languages like Scala and functional Java and is also familiar with other programming languages such as Java, C, C++, HTML. He is currently working on reactive technologies like Spark, Kafka, Akka, Lagom, Cassandra and also used DevOps tools like DC/OS and Mesos for Deployments. His hobbies include watching movies, anime and he also loves travelling a lot.

Leave a Reply

%d bloggers like this: