Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does => mean at the beginning of a Scala class definition?

Tags:

The author of the question Exchanging type parameters with abstract types wrote a => at the beginning of his class definitions. Example:

abstract class Thing { t =>    type A    type G <: Group { type A = t.A }    val group: G  }  

What does the t => mean ?

Because this is hard to find in Google & Co, can someone please give me more background information or provide a link, where I can find more information about this language construct ?

like image 330
John Threepwood Avatar asked Jun 29 '12 18:06

John Threepwood


People also ask

What does => mean in Scala?

=> is the "function arrow". It is used both in function type signatures as well as anonymous function terms. () => Unit is a shorthand for Function0[Unit] , which is the type of functions which take no arguments and return nothing useful (like void in other languages).

How do you define a class in Scala?

Defining a classA minimal class definition is simply the keyword class and an identifier. Class names should be capitalized. The keyword new is used to create an instance of the class. We call the class like a function, as User() , to create an instance of the class.

What is Scala object vs class?

Difference Between Scala Classes and Objects Definition: A class is defined with the class keyword while an object is defined using the object keyword. Also, whereas a class can take parameters, an object can't take any parameter. Instantiation: To instantiate a regular class, we use the new keyword.

How do you call an object class in Scala?

In Scala, an object of a class is created using the new keyword. The syntax of creating object in Scala is: Syntax: var obj = new Dog();


1 Answers

The default naming for class itself is this. You may replace it with t by t =>

It is useful if your class contains subclasses and you need access to enclosing self reference.

Without t => in your example you would write something like this:

abstract class Thing {   type G <: Group { type A = this.A } } 

Group { type A = this.A } is a subtype so this would reference to group specialization itself not to a thing object. Probably you get not what you mean to get. If you need access to Thing self reference you should resolve name conflict by assigning self reference another name

abstract class Thing { another_this = >   type G <: Group { type A = another_this.A} } 
like image 104
ayvango Avatar answered Nov 11 '22 17:11

ayvango