24

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 ?

Community
  • 1
  • 1
John Threepwood
  • 15,593
  • 27
  • 93
  • 149
  • 1
    It is a "self type" or "[typed] self reference", as in http://www.scala-lang.org/node/124 and a reason for use http://stackoverflow.com/questions/1990948/what-is-the-difference-between-scala-self-types-and-trait-subclasses (The most authoritative guide is arguably the Scala Language Specification or SLS.) –  Jun 29 '12 at 18:35
  • 2
    http://stackoverflow.com/questions/4017357/difference-between-this-and-self-in-self-type-annotations , http://stackoverflow.com/tags/scala/info (look for "self") –  Jun 29 '12 at 18:54

2 Answers2

16

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}
}
ayvango
  • 5,867
  • 3
  • 34
  • 73
  • As far as I can see, there are 'self types' which pst mentioned in his comments and there is the other use of just renaming the 'this' reference as you mentioned. Are both called 'self types' ? – John Threepwood Jun 30 '12 at 12:16
  • 1
    after class header and before class body there is space for two declaration: you may specify alias for this reference, as it was demonstrated, and you may specify self type as it described in suggested links, or you may specify both. However self type declaration is rather cumbersome and require you to write a name. You may use `this : AType =>` if you would like to specify only self type and you can write `alias : AType =>` if you wand specify both alias and self type. Technically self type is a type, but self type declaration includes alias definition – ayvango Jun 30 '12 at 13:27
0

It is indeed self type annotation. See the official Scala specification:

https://scala-lang.org/files/archive/spec/2.13/13-syntax-summary.html

According to this specification, its context free EBNF syntax is:

SelfType ::= id [‘:’ Type] ‘=>’ | ‘this’ ‘:’ Type ‘=>’ So, basically, this means SelfType has two basic forms. In one form, you can use an id with or without Type. In the other, you can use this but it must be accompanied with a Type.

You can find it in section 29.4 of Programming in Scala Second Edition. However, remember that books can be quickly out of date, so you need to refer to the specification.

Yang
  • 77
  • 7