`#` 运算符在 Scala 中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9443004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the `#` operator mean in Scala?
提问by Freewind
I see this code in this blog: Type-Level Programming in Scala:
我在此博客中看到此代码:Scala 中的类型级编程:
// 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]
}
There is an operator #in the code R#X[R#Next]which I've never seen. Since it's difficult to search it(ignored by search engines), who can tell me what does it mean?
#代码中有一个R#X[R#Next]我从未见过的运算符。由于很难搜索(被搜索引擎忽略),谁能告诉我这是什么意思?
回答by Daniel C. Sobral
To explain it, we first have to explain nested classes in Scala. Consider this simple example:
为了解释它,我们首先必须解释 Scala 中的嵌套类。考虑这个简单的例子:
class A {
class B
def f(b: B) = println("Got my B!")
}
Now let's try something with it:
现在让我们尝试一下:
scala> val a1 = new A
a1: A = A@2fa8ecf4
scala> val a2 = new A
a2: A = A@4bed4c8
scala> a2.f(new a1.B)
<console>:11: error: type mismatch;
found : a1.B
required: a2.B
a2.f(new a1.B)
^
When you declare a class inside another class in Scala, you are saying that each instanceof that class has such a subclass. In other words, there's no A.Bclass, but there are a1.Band a2.Bclasses, and they are differentclasses, as the error message is telling us above.
当您在 Scala 中的另一个类中声明一个类时,您是在说该类的每个实例都有这样一个子类。换句话说,有没有A.B类,但有a1.B和a2.B类,它们是不同的类,错误消息告诉我们之上。
If you did not understand that, look up path dependent types.
如果您不明白这一点,请查找路径相关类型。
Now, #makes it possible for you to refer to such nested classes without restricting it to a particular instance. In other words, there's no A.B, but there's A#B, which means a Bnested class of anyinstance of A.
现在,#您可以在不将其限制为特定实例的情况下引用此类嵌套类。换句话说,没有A.B,但是有A#B,这意味着B的任何实例的嵌套类A。
We can see this in work by changing the code above:
我们可以通过更改上面的代码来看到这一点:
class A {
class B
def f(b: B) = println("Got my B!")
def g(b: A#B) = println("Got a B.")
}
And trying it out:
并尝试一下:
scala> val a1 = new A
a1: A = A@1497b7b1
scala> val a2 = new A
a2: A = A@2607c28c
scala> a2.f(new a1.B)
<console>:11: error: type mismatch;
found : a1.B
required: a2.B
a2.f(new a1.B)
^
scala> a2.g(new a1.B)
Got a B.
回答by missingfaktor
It's known as type projection, and is used to access type members.
它被称为类型投影,用于访问类型成员。
scala> trait R {
| type A = Int
| }
defined trait R
scala> val x = null.asInstanceOf[R#A]
x: Int = 0
回答by moveaway00
Basically, it's a way of referring to classes within other classes.
基本上,它是在其他类中引用类的一种方式。
http://jim-mcbeath.blogspot.com/2008/09/scala-syntax-primer.html(search for "pound")
http://jim-mcbeath.blogspot.com/2008/09/scala-syntax-primer.html(搜索“磅”)
回答by Gene T
Here's a resource for searching on "symbolic operators" (which are really methods), but I haven't figured out how to escape "#" to search on in scalex)
这是用于搜索“符号运算符”(实际上是方法)的资源,但我还没有弄清楚如何转义“#”以在 scalex 中搜索)

