斯卡拉分配评估单元的动机是什么,而不是分配的值? [英] What is the motivation for Scala assignment evaluating to Unit rather than the value assigned?
问题描述
Scala赋值对Unit的评价动机是什么,而不是赋值?
I / O编程中的一个常见模式是做这样的事情:< /(b) 但是在Scala中这是不可能的,因为... .. returns单位,而不是bytesRead的新值。 看起来像是一个有趣的事情,可以省略函数式语言。 我主张赋值返回赋值而不是单位。马丁和我之间来回转移,但他的观点是,为了在95%的时间内实现价值而忽略字节码并对性能产生负面影响。 What is the motivation for Scala assignment evaluating to Unit rather than the value assigned? A common pattern in I/O programming is to do things like this: But this is not possible in Scala because... .. returns Unit, not the new value of bytesRead. Seems like an interesting thing to leave out of a functional language.
I am wondering why it was done so? I advocated for having assignments return the value assigned rather than unit. Martin and I went back and forth on it, but his argument was that putting a value on the stack just to pop it off 95% of the time was a waste of byte-codes and have a negative impact on performance. 这篇关于斯卡拉分配评估单元的动机是什么,而不是分配的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
bytesRead = in.read(buffer)
我想知道为什么这样做?while ((bytesRead = in.read(buffer)) != -1) { ...
bytesRead = in.read(buffer)