Open In App

Scala | Self types Annotation

Last Updated : 25 Apr, 2019
Improve
Improve
Like Article
Like
Save
Share
Report


A self type annotation of a trait is the assumed type of this, within a trait, the receiver to be used. Any concrete class that mixes in a trait must ensure that its self type conforms to the trait that is mixed in. That means using self types does not expose local variable and methods to its sub-classes and sub-traits. For dividing a large class into several traits is the most common use of self types. Self types are a way to declare that a trait must be mixed into another trait or class, even though it doesn’t directly extend it, that makes the members of the dependency of those mixed in traits available without import.

To use a self type in a trait write identifier_name, next to it the type of trait to mix in, and following => symbol; as given below.
Syntax:

trait A{
    //statements
}
trait B{
    this: A => //here we can use methods & variables of trait A
}

Let’s discuss some examples.
Example :




// Scala Program that uses self type
trait with_powers
    var mind ="extra-ordinary"
      
}
trait without_powers
{
    var mind="ordinary";
}
trait person
    def brain();
}
  
// class extend trait
class extraordinary_person extends person
{   
    // reassign this
    this: with_powers => 
    override def brain() = println(s"super hero brain is $mind!");
}
  
// class extend trait
class ordinary_person extends person
{   
    // reassign this
    this: without_powers => 
    override def brain() = println(s"normal human brain is $mind.");
}
  
// Creating object
object GFG
{
    // Main method
    def main(args:Array[String])
    {
        val hero = new extraordinary_person() with with_powers;
        val mohan = new ordinary_person() with without_powers;
          
        //val mohan= new ordinary_person() with with_powers; ERROR
        //does not conform to ordinary_person's self type
        hero.brain();
        mohan.brain();
    }
}


Output :

super hero brain is extra-ordinary!.
normal human brain is ordinary.

In above example we create traits with_powers, without_powers and person. here class extraordinary_person extends trait person. similarly class ordinary_person also extends trait person. self type annotation happens in both class by using this trait name =>
val mohan= new ordinary_person() with with_powers; shows an error does not conform to ordinary_person’s self type .
Example :




// Scala Program that uses self type
trait A
    def x = 1;
}
  
// trait extend another trait
trait B extends A
{
    override def x = super.x * 5;
}
  
// trait extend another trait
trait C1 extends B
{
    override def x = 2;
}
  
// trait extend another trait
trait C2 extends A
    this: B=> override def x = 2
      
}
  
// Creating object
object GFG
{
    // Main method
    def main(args:Array[String])
    {
        println((new C1 with B).x);
        println((new C2 with B).x);
    }
}


Output :

2
10

Here, in above example C2 is an abstract type and self type of C2 with B. ((new C2 with B)) conforms to C2’s self type with B (this: B=>) So, a self type lets you specify what types of traits are allowed to mixin.

Example :




// Scala Program that uses self type
trait Being
{
    var name: String
    var age: Int
    var gender: String
}
  
// Creating trait
trait Sayings
{
    this: Being =>
    def greetings(greet: String) = println(s"$name: $greet");
    def info() = println(s"I am $age years old $gender");
}
  
// extend both trait with single class
class Person(var r_name: String, var r_age: Int, 
                            var r_gender: String)
extends Sayings with Being
{
    var name = s"Person $r_name";
    var age = r_age;
    var gender = r_gender;
}
  
// Creating object
object GFG
{
    // Main method
    def main(args: Array[String])
    {
        val person = new Person("Lakshya", 24, "Male");
        person.greetings("hello, hi there!!");
        person.info();
    }
}


Output :

Person Lakshya: hello, hi there!!
I am 24 years old Male

In above example (this: Being => )it basically means that variables name, age, gender of Being are now in the scope of greetings and info methods but without exposing them to any other subclass or trait that extends it. So a self type specifies the requirements on any concrete class the type of trait should be mixed in.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads