什么是=>是否在Scala类定义的开始? [英] What does => mean at the beginning of a Scala class definition?

查看:113
本文介绍了什么是=>是否在Scala类定义的开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题作者
使用抽象类型交换类型参数

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 
} 

t => 是什么意思?

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

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 ?

推荐答案

类本身的默认命名是 this 。您可以用 t 替换 t =>

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.

没有 t => 在你的例子中,你会写下这样的:

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

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

类型A = this.A} 是一个子类型,因此 this 会引用组特化本身而不是一个事物对象。可能你不会得到你的意思。如果您需要访问Thing自我参考,您应该通过分配自我引用另一个名称来解决名称冲突。

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}
}

这篇关于什么是=&gt;是否在Scala类定义的开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆