奥德斯基对“账单!*&^%~代码!”是认真的吗?
在他的《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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
奥德斯基的意思是,可能会有这样的有效代码。例如,下面的代码:
只需将其复制并粘贴到 REPL 上即可。
What Odersky means to say is that it would be possible to have valid code looking like that. For instance, the code below:
Just copy&paste it on the REPL.
对于调用采用单个参数或具有空参数列表的方法,句点是可选的。
使用此功能时,方法名称后面的空格后面的下一个块被假定为单个参数。
因此,
(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.
我不确定这本书是否提供了方法签名,但我认为这只是对 Scala 语法糖的评论,因此它假设您输入:
其中有一个对象帐单,其中有一个方法 add ,该方法接受一个参数,然后它会自动将其解释为:
由于 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:
where there is an object bill which has a method add which takes a parameter then it automatically interprets it as:
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.
可选的“.()”与方法调用(如上面 Wysawyg 所解释的)的组合以及使用(几乎)您喜欢的任何字符来命名方法的能力,使得在 Scala 中编写看起来像运算符重载的方法成为可能。您甚至可以发明自己的运算符。
例如,我有一个处理 3D 计算机图形的程序。我有自己的类
Vector
来表示 3D 矢量:我还定义了一个方法
**
(上面未显示)来计算 两个向量的叉积。您可以像 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: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.