Skip to main content
 首页 » 编程设计

class之=> 在 Scala 类定义的开头是什么意思

2024年12月31日4yxwkf

问题的作者
Exchanging type parameters with abstract types写了一个 =>在他的类定义的开头。例子:

abstract class Thing { t =>  
  type A  
  type G <: Group { type A = t.A }  
  val group: G  
}  
t =>有什么用意思 ?

因为这在 Google & Co 中很难找到,有人可以给我更多背景信息或提供链接,我可以在哪里找到有关此语言结构的更多信息?

请您参考如下方法:

类本身的默认命名是 this .您可以将其替换为 t来自 t =>
如果您的类包含子类并且您需要访问封闭的自引用,这将很有用。

t =>在您的示例中,您将编写如下内容:

abstract class Thing { 
  type G <: Group { type A = this.A } 
} 
Group { type A = this.A }是一个子类型,所以 this将引用组特化本身而不是事物对象。可能你得到的不是你想要得到的。如果您需要访问 Thing self reference,您应该通过为 self reference 分配另一个名称来解决名称冲突
abstract class Thing { another_this = > 
  type G <: Group { type A = another_this.A} 
}