简体   繁体   English

A#B在scala中是什么意思

[英]what does A#B mean in scala

I am trying to understand the following piece of code. 我试图理解以下代码。 but I don't know what the R#X mean. 但我不知道R#X是什么意思。 could someone help me? 有人可以帮我吗?

 // define the abstract types and bounds
 trait Recurse {
    type Next <: Recurse
// this is the recursive function definition
    type X[R <: Recurse] <: Int
 }
// implementation
 trait RecurseA extends Recurse {
    type Next = RecurseA
  // this is the implementation
    type X[R <: Recurse] = R#X[R#Next]
 }
object Recurse {
  // infinite loop
  type C = RecurseA#X[RecurseA]
}

You may gain type from existing instance of a class: 您可以从类的现有实例获取类型:

class C {
  type someType = Int
}
val c = new C
type t = c.someType

Or may address to the type directly without instantiating an object: C#someType This form is very usefull for type expressions where you have no space to create intermediate variables. 或者可以在不实例化对象的情况下直接寻址该类型: C#someType这种形式对于没有空间创建中间变量的类型表达式非常有用。


Adding some clarifications as it was suggested in comments. 添加了一些评论中建议的澄清。

Disclaimer : I have only partial understanding of how Scala's type system works. 免责声明 :我对Scala的类型系统如何工作只有部分了解。 I'd tried to read documentation several times, but was able to extract only patchy knowledges from it. 我曾尝试阅读过几次文档,但只能从中提取一些零碎的知识。 But I have rich experience in scala and may predict compilers behavior on individual cases well. 但是我在scala方面拥有丰富的经验,并且可以很好地预测编译器在个别情况下的行为。

# called type projection and type projection compliments normal hierarchical type access via . #称为类型投影和类型投影补充了通过进行普通的分层类型访问. In every type expression scala implicitly uses both. 在每个类型表达式中,scala都隐式使用两者。

scala reference gives examples of such invisible conversions: scala参考提供了此类不可见转换的示例:

t                        ə.type#t
Int                      scala.type#Int
scala.Int                scala.type#Int
data.maintable.Node      data.maintable.type#Node

As use see, every trivial usage of type projection actually works on type (that is return with .type ) not on an object. 如使用所见,类型投影的所有琐碎用法实际上都适用于类型(即以.type返回),而不适用于对象。 The main practical difference (I'm bad with definitions) is that object type is something ephemeral as object itself is. 主要的实际区别(我对定义不好)是对象类型就像对象本身一样短暂。 Its type may be changed in appropriate circumstances such as inheritance of an abstract class type. 在诸如抽象类类型的继承之类的适当情况下,可以更改其类型。 In contrast type's type (the definition of the type projection) is as stable as sun. 对比类型的类型(类型投影的定义)与太阳一样稳定。 Types (don't mix them with classes) in scala are not first-class citizens and can not be overridden further. Scala中的类型(不要将它们与类混用)不是一等公民,并且不能被进一步覆盖。

There are different places suitable for putting type expression into. 有许多适合放置类型表达式的地方。 There are also some places where only stable types are allowed. 在某些地方,仅允许使用稳定类型。 So basically type projection is more constant for terms of type. 因此,基本上,对于类型而言,类型投影更恒定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM