super 不在操作员位置?

发布于 2024-11-01 23:49:03 字数 476 浏览 0 评论 0原文

在 Scala 中,当方法调用位于操作符位置时,调用方法是有语法糖的。例如(5).+(3)可以写成5 + 3。其规则是.()。如果这条规则成立,则可以保留点和括号。但我发现了这个规则的一个例外:它无法在运算符位置写入 super 关键字:

class X {
  def x(i: Int) = i * 2
}
class Y extends X {
  override def x(i: Int) = super.x(i + 1)
}

当我将点留在 super 后面时,我会收到编译器错误。在这里,super 是对象,x 是方法,i + 1 是参数,因此我不明白为什么会出现错误。谁能解释一下吗?

in Scala there is syntactic sugar by calling a method when the method call is in operator position. For example (5).+(3) can be written as 5 + 3. The rule for this is <object>.<method>(<parameter>). It this rule is true the dot and the parentheses can be left. But I found an exception of this rule: it is not able to write the super-keyword in operator position:

class X {
  def x(i: Int) = i * 2
}
class Y extends X {
  override def x(i: Int) = super.x(i + 1)
}

When I leave the dot behind the super I get a compiler error. Here, super is the object, x the method and i + 1 the parameter therefore I don't understand why I get an error. Can anyone explain it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖公主 2024-11-08 23:49:03

我相信这是因为 super 关键字必须(但请参阅 huynhjl 的评论和下面的详细信息)后跟一个 . ,然后跟一个有效的成员标识符 -- < em>这似乎是语法产生规则。

考虑到 val expr = (super) 没有任何有用的含义,并且应该始终是无效的(在这方面,它与 this 完全不同,后者本身就是一个有效的表达式)。在语法级别要求 super.member 形式可能是有意为之,但这需要一些挖掘。根据规范的措辞(请参阅 huynhjl 的评论)和 per语法“摘要”规则。

class X {
  def x(): X = this
}
class Y extends X {
  // Still kaboom of course -- message is effectively the same
  // and indicates a grammar production has been violated.
  // Message: error: '.' expected but '}' found.
  override def x(): X = super
}

快乐编码。


更新(参考资料):

来自 Scala 语言规范 2.8 讨论 第 6.5 节中的 super

<块引用>

引用 super.m 以最不恰当的方式静态引用方法或类型 m
包含引用的最内层模板的超类型。它评估为
该模板的实际超类型中的成员等于 m
覆盖m静态引用的成员m必须是类型或方法
...
super 前缀后面可能跟有特征限定符 [T],如 C.super[T].x 中所示。这
称为静态超级引用。在这种情况下,引用的是类型或方法
x 位于 C 的父特征中,其简单名称为 T

摘自 Scala 语法摘要(A 章):

stableId ::= id
           | Path ‘.’ id
           | [id ’.’] ‘super’ [ClassQualifier] ‘.’ id

I believe it is because the super keyword must (but see the comment by huynhjl and details below) be followed with a . and then followed with a valid member identifier -- it seems to be grammar production rule.

Consider that val expr = (super) has no useful meaning and should always be invalid (in this regard it is quite different from this which is a valid expression by itself). Requiring the super.member form at the grammar level is likely intentional, but it would require some digging. intentional per the wording the specification (see the comment by huynhjl) and per the grammar "summary" rules.

class X {
  def x(): X = this
}
class Y extends X {
  // Still kaboom of course -- message is effectively the same
  // and indicates a grammar production has been violated.
  // Message: error: '.' expected but '}' found.
  override def x(): X = super
}

Happy coding.


Update (references):

From the Scala Language Specification 2.8 discussing super in Section 6.5:

A reference super.m refers statically to a method or type m in the least proper
supertype of the innermost template containing the reference. It evaluates to the
member in the actual supertype of that template which is equal to m or which
overrides m. The statically referenced member m must be a type or a method
...
The super prefix may be followed by a trait qualifier [T], as in C.super[T].x. This
is called a static super reference. In this case, the reference is to the type or method of
x in the parent trait of C whose simple name is T.

And from the Scala Syntax Summary (Chapter A):

stableId ::= id
           | Path ‘.’ id
           | [id ’.’] ‘super’ [ClassQualifier] ‘.’ id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文