Scala中的增量(++)运算符 [英] Increment (++) operator in Scala

查看:81
本文介绍了Scala中的增量(++)运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Scala是否有任何理由不支持++运算符默认情况下增加基本类型? 例如,您不能写:

Is there any reason for Scala not support the ++ operator to increment primitive types by default? For example, you can not write:

var i=0
i++

谢谢

推荐答案

我的猜测是省略了它,因为它仅适用于可变变量,而对于不可变的值则没有意义.也许已经决定++运算符不尖叫分配,所以包括它可能会导致您是否正在对该变量进行变异的错误.

My guess is this was omitted because it would only work for mutable variables, and it would not make sense for immutable values. Perhaps it was decided that the ++ operator doesn't scream assignment, so including it may lead to mistakes with regard to whether or not you are mutating the variable.

我认为可以安全地执行这样的操作(一行):

I feel that something like this is safe to do (on one line):

i++

但这是一个不好的做法(无论使用哪种语言):

but this would be a bad practice (in any language):

var x = i++

您不想混合分配语句和副作用/突变.

You don't want to mix assignment statements and side effects/mutation.

这篇关于Scala中的增量(++)运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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