禁止显示“丢弃的非单位值”。警告 [英] Suppress "discarded non-Unit value" warning
本文介绍了禁止显示“丢弃的非单位值”。警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已在我的构建中添加了scalac命令行参数 -Ywarn-value-discard
,因为这会捕获我在代码中发现的一个细微错误。但是,我现在收到有关丢弃的非单位值的警告,这些警告与故意丢弃而不是错误有关。我该如何禁止这些警告?
I have added the scalac command line argument -Ywarn-value-discard
to my build because this would have caught a subtle bug that I just found in my code. However, I now get some warnings for "discarded non-Unit value" that are about intentional discards, not bugs. How do I suppress those warnings?
推荐答案
您可以通过明确返回单位(即( )
)。
例如,将其变为:
You suppress these warning by explictly returning unit (that is ()
).
By example turn this:
def method1() = {
println("Hello")
"Bye"
}
def method2() {
method1() // Returns "Bye", which is implicitly discarded
}
进入:
def method1() = {
println("Hello")
"Bye"
}
def method2() {
method1()
() // Explicitly return unit
}
这篇关于禁止显示“丢弃的非单位值”。警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文