What does => mean at the beginning of a Scala clas

2019-03-17 09:38发布

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 ?

标签: class scala
1条回答
男人必须洒脱
2楼-- · 2019-03-17 10:21

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}
}
查看更多
登录 后发表回答