斯卡拉分配评估单元的动机是什么,而不是分配的值? [英] What is the motivation for Scala assignment evaluating to Unit rather than the value assigned?

查看:147
本文介绍了斯卡拉分配评估单元的动机是什么,而不是分配的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Scala赋值对Unit的评价动机是什么,而不是赋值?



I / O编程中的一个常见模式是做这样的事情:< /(b)

但是在Scala中这是不可能的,因为...

  bytesRead = in.read(buffer)

.. 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:

while ((bytesRead = in.read(buffer)) != -1) { ...

But this is not possible in Scala because...

bytesRead = in.read(buffer)

.. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆