奥德斯基对“账单!*&^%~代码!”是认真的吗?

发布于 2024-08-30 04:13:58 字数 244 浏览 10 评论 0原文

在他的《scala 编程》一书中(第 5 章第 5.9 节第 93 页) Odersky

在同一页的脚注中提到了这个表达式“bills !*&^%~ code!”:

“现在你应该能够弄清楚,给定这段代码,Scala 编译器将

调用(bills.!*&^%~(code)).!()."

这对我来说有点神秘,有人可以解释一下这里发生了什么吗?

In his book programming in scala (Chapter 5 Section 5.9 Pg 93)
Odersky mentioned this expression "bills !*&^%~ code!"

In the footnote on same page:

"By now you should be able to figure out that given this code,the Scala compiler would

invoke (bills.!*&^%~(code)).!()."

That's a bit to cryptic for me, could someone explain what's going on here?

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

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

发布评论

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

评论(4

风流物 2024-09-06 04:13:58

奥德斯基的意思是,可能会有这样的有效代码。例如,下面的代码:

class BadCode(whose: String, source: String) {
  def ! = println(whose+", what the hell do you mean by '"+source+"'???")
}

class Programmer(who: String) {
  def !*&^%~(source: String) = new BadCode(who, source)
}

val bills = new Programmer("Bill")
val code = "def !*&^%~(source: String) = new BadCode(who, source)"
bills !*&^%~ code!

只需将其复制并粘贴到 REPL 上即可。

What Odersky means to say is that it would be possible to have valid code looking like that. For instance, the code below:

class BadCode(whose: String, source: String) {
  def ! = println(whose+", what the hell do you mean by '"+source+"'???")
}

class Programmer(who: String) {
  def !*&^%~(source: String) = new BadCode(who, source)
}

val bills = new Programmer("Bill")
val code = "def !*&^%~(source: String) = new BadCode(who, source)"
bills !*&^%~ code!

Just copy&paste it on the REPL.

随梦而飞# 2024-09-06 04:13:58

对于调用采用单个参数或具有空参数列表的方法,句点是可选的。

使用此功能时,方法名称后面的空格后面的下一个块被假定为单个参数。

因此,

(bills.!*&^%~(code)).!()。

与账单相同

!*&^%~ 代码!

第二个感叹号对第一个方法调用的返回值调用一个方法。

The period is optional for calling a method that takes a single parameter, or has an empty parameter list.

When this feature is utilized, the next chunk after the space following the method name is assumed to be the single parameter.

Therefore,

(bills.!*&^%~(code)).!().

is identical to

bills !*&^%~ code!

The second exclamation mark calls a method on the returned value from the first method call.

梦年海沫深 2024-09-06 04:13:58

我不确定这本书是否提供了方法签名,但我认为这只是对 Scala 语法糖的评论,因此它假设您输入:

bill add monkey

其中有一个对象帐单,其中有一个方法 add ,该方法接受一个参数,然后它会自动将其解释为:

bill.add(monkey)

由于 Scala 有点生疏,我不完全确定它如何分割代码!进入 (code).!() ,除了 ! 的灰色单元格模糊地发痒。运算符用于触发参与者,在编译器术语中,该参与者可能被解释为对象上的隐式 .!() 方法。

I'm not sure if the book provides method signatures but I assume it's just a comment on Scala's syntactic sugar so it assumes if you type:

bill add monkey

where there is an object bill which has a method add which takes a parameter then it automatically interprets it as:

bill.add(monkey)

Being a little Scala rusty, I'm not entirely sure how it splits code! into (code).!() except for a vague tickling of the grey cells that the ! operator is used to fire off an actor which in compiler terms might be interpretted as an implicit .!() method on the object.

蓝礼 2024-09-06 04:13:58

可选的“.()”与方法调用(如上面 Wysawyg 所解释的)的组合以及使用(几乎)您喜欢的任何字符来命名方法的能力,使得在 Scala 中编写看起来像运算符重载的方法成为可能。您甚至可以发明自己的运算符。

例如,我有一个处理 3D 计算机图形的程序。我有自己的类 Vector 来表示 3D 矢量:

class Vector(val x: Double, val y: Double, val z: Double) {
    def +(v: Vector) = new Vector(x + v.x, y + v.y, z + v.z)

    // ...etc.
}

我还定义了一个方法 ** (上面未显示)来计算 两个向量的叉积。您可以像 Scala 中那样创建自己的运算符,这非常方便,没有多少其他编程语言具有这种灵活性。

The combination of the '.()' being optional with method calls (as Wysawyg explained above) and the ability to use (almost) whatever characters you like for naming methods, makes it possible to write methods in Scala that look like operator overloading. You can even invent your own operators.

For example, I have a program that deals with 3D computer graphics. I have my own class Vector for representing a 3D vector:

class Vector(val x: Double, val y: Double, val z: Double) {
    def +(v: Vector) = new Vector(x + v.x, y + v.y, z + v.z)

    // ...etc.
}

I've also defined a method ** (not shown above) to compute the cross product of two vectors. It's very convenient that you can create your own operators like that in Scala, not many other programming languages have this flexibility.

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