问题描述
在我的scala测试文件中,我有很多nullary方法(带有0个参数的方法)。因此,不要将它们写成:
def fooBar()= //
我将它们写成:
def fooBar = //
当我这样做时,我收到以下警告:
警告:不鼓励(22,7)副作用的暗方法:建议将def fooBar()定义为
警告的含义是什么?我正在使用intelliJ作为我的IDE,并且无法真正在Web上发现这个警告。
编辑
而且,我忘了提及,当我使用方括号时,不会出现警告。
解决方案nullary方法的常见约定是:
- 如果它是副作用方法,则表示它使用括号
- 否则,在没有副作用的情况下使用纯访问器类方法时,可以使用括号括起来
您违反了这条规则,IDE会警告您。
另请参阅
I have a lot of nullary methods (methods with 0 parameters) in my scala test file. Hence, instead of writing them as :
def fooBar() = //
I write them as :
def fooBar = //
I get the following warning when I do so:
Warning:(22, 7) side-effecting nullary methods are discouraged: suggest defining as `def fooBar()` instead
What is the meaning of the warning? I am using intelliJ as my IDE and could not really find much about this warning on the web.
EDIT
And, I forgot to mention, when I use the brackets, the warning does not appear.
The common convention for nullary methods is to:
- in case it's a side-effecting method, signify it with use of parenthesis
- otherwise, drop parenthesis in case it's pure accessor-like method with no side effects
You're breaking this rule and IDE warns you about this.
See also https://stackoverflow.com/a/7606214/298389
这篇关于以下警告的含义是什么:“不鼓励使用副作用的无效方法”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!