转载

What is the motivation for Scala assignment evaluating to Unit rather than the value assigned?


问题:

What is the motivation for Scala assignment evaluating to Unit rather than the value assigned? 将Scala分配评估为单位而不是赋值的动机是什么?

A common pattern in I/O programming is to do things like this: I / O编程中的常见模式是执行以下操作:

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

But this is not possible in Scala because... 但这在Scala中是不可能的,因为...

bytesRead = in.read(buffer)

.. returns Unit, not the new value of bytesRead. ..返回Unit,而不是bytesRead的新值。

Seems like an interesting thing to leave out of a functional language. 忽略功能性语言似乎是一件有趣的事情。 I am wondering why it was done so? 我想知道为什么要这么做吗?

解决方案:

参考: https://stackoom.com/en/question/8NxU
{{o.name}}
{{m.name}}
  • 转载自:https://my.oschina.net/u/4437974/blog/5393808
  • 留言