Scala | Self types Annotation
Last Updated :
25 Apr, 2019
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 :
trait with _ powers
{
var mind = "extra-ordinary" ;
}
trait without _ powers
{
var mind = "ordinary" ;
}
trait person
{
def brain();
}
class extraordinary _ person extends person
{
this : with _ powers =>
override def brain() = println(s "super hero brain is $mind!" );
}
class ordinary _ person extends person
{
this : without _ powers =>
override def brain() = println(s "normal human brain is $mind." );
}
object GFG
{
def main(args : Array[String])
{
val hero = new extraordinary _ person() with with _ powers;
val mohan = new ordinary _ person() with without _ powers;
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 :
trait A
{
def x = 1 ;
}
trait B extends A
{
override def x = super .x * 5 ;
}
trait C 1 extends B
{
override def x = 2 ;
}
trait C 2 extends A
{
this : B => override def x = 2 ;
}
object GFG
{
def main(args : Array[String])
{
println(( new C 1 with B).x);
println(( new C 2 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 :
trait Being
{
var name : String
var age : Int
var gender : String
}
trait Sayings
{
this : Being =>
def greetings(greet : String) = println(s "$name: $greet" );
def info() = println(s "I am $age years old $gender" );
}
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;
}
object GFG
{
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.
Share your thoughts in the comments
Please Login to comment...