何时在 Scala 方法声明中使用等号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/944111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to use the equals sign in a Scala method declaration?
提问by Esko Luontola
With equals sign:
带等号:
object HelloWorld {
def main(args: Array[String]) = {
println("Hello!")
}
}
Without equals sign:
没有等号:
object HelloWorld {
def main(args: Array[String]) {
println("Hello!")
}
}
Both of the above programs execute the same way. In the blog post Things I do not like in ScalaI read that when the equals sign are missing, the method will return Unit(same as Java's void), so methods that return a value must use the equals sign. But methods that don't return a value can be written either way.
上述两个程序的执行方式相同。在博客文章我不喜欢 Scala 的事情中,我读到当缺少等号时,该方法将返回Unit(与 Java 的 相同void),因此返回值的方法必须使用等号。但是不返回值的方法可以用任何一种方式编写。
What is the best practice for using the equals sign in Scala methods that don't return a value?
在不返回值的 Scala 方法中使用等号的最佳实践是什么?
回答by Jorge Ortiz
I actually disagree pretty strongly with Daniel. I think the non-equal syntax should neverbe used. If your method is being exposed as an API and you're worried about accidentally returning the wrong type, add an explicit type annotation:
我实际上非常不同意丹尼尔。我认为不相等的语法应该永远不会被使用。如果您的方法作为 API 公开并且您担心不小心返回错误的类型,请添加显式类型注释:
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello!")
123
}
}
The non-equal syntax is shorter and might look "cleaner", but I think it just adds the possibility of confusion. I have sometimes forgotten to add an equal sign and believed my method was returning a value when actually it was returning Unit. Because the non-equal and equal-with-inferred-type syntaxes are so visually similar, it's easy to miss this problem.
non-equal 语法更短,可能看起来“更干净”,但我认为它只会增加混淆的可能性。有时我忘记添加等号,并认为我的方法实际上返回了一个值,而实际上它返回了 Unit。因为 non-equal 和 equal-with-inferred-type 语法在视觉上非常相似,所以很容易错过这个问题。
Even though it costs me a little more work, I prefer the explicit type annotations when they matter (namely, exposed interfaces).
尽管它花费了我更多的工作,但我更喜欢显式类型注释,当它们很重要时(即暴露的接口)。
回答by Daniel Spiewak
UPDATE: as of Scala-2.10, using equals sign is preferred. Old answer:
更新:从 Scala-2.10 开始,首选使用等号。旧答案:
Methods which return Unitshould alwaysuse the non-equals syntax. This avoids potential mistakes in implementation carrying over into the API. For example, you could have accidentally done something like this:
返回的方法Unit应始终使用非等于语法。这避免了在实现过程中遗留到 API 中的潜在错误。例如,你可能不小心做了这样的事情:
object HelloWorld {
def main(args: Array[String]) = {
println("Hello!")
123
}
}
Trivial example of course, but you can see how this might be a problem. Because the last expression does notreturn Unit, the method itself will have a return type other than Unit. This is exposed in the public API, and might cause other problems down the road. With the non-equals syntax, it doesn't matter what the last expression is, Scala fixes the return type as Unit.
当然,这是一个微不足道的例子,但您可以看到这可能是一个问题。由于最后一个表达式并没有返回Unit,该方法本身具有比其他返回类型Unit。这在公共 API 中公开,并且可能会导致其他问题。使用非等式语法,最后一个表达式是什么并不重要,Scala 将返回类型固定为Unit.
It's also two characters cleaner. :-) I also tend to think that the non-equals syntax makes the code just a little easier to read. It is more obvious that the method in question returns Unitrather than some useful value.
它也是两个字符更清洁。:-) 我也倾向于认为 non-equals 语法使代码更容易阅读。更明显的是,所讨论的方法返回Unit而不是一些有用的值。
On a related note, there is an analogous syntax for abstract methods:
在相关说明中,抽象方法有一个类似的语法:
trait Foo {
def bar(s: String)
}
The method barhas signature String=>Unit. Scala does this when you omit the type annotation on an abstract member. Once again, this is cleaner, and (I think) easier to read.
该方法bar具有签名String=>Unit。当您省略抽象成员上的类型注释时,Scala 会这样做。再一次,这更干净,而且(我认为)更容易阅读。
回答by Daniel C. Sobral
You mustuse equals sign in call declarations except the definitions returning Unit.
您必须在调用声明中使用等号,但返回 Unit 的定义除外。
In this latter case, you mayforgo the equals sign. This syntax may be deprecated, though, so it's best avoided. Using equals sign and declaring the return type will always work.
在后一种情况下,您可以放弃等号。但是,此语法可能已被弃用,因此最好避免使用。使用等号并声明返回类型将始终有效。
回答by cmd
For methods, Scala Style Guiderecommends the equals syntax as opposed to procedure syntax
对于方法,Scala 风格指南推荐使用 equals 语法而不是过程语法
Procedure Syntax
Avoid the procedure syntax, as it tends to be confusing for very little gain in brevity.
过程语法
避免使用过程语法,因为它在简洁性方面往往会让人感到困惑。
// don't do this
def printBar(bar: Baz) {
println(bar)
}
// write this instead
def printBar(bar: Bar): Unit = {
println(bar)
}
回答by sofiene zaghdoudi
for method that dont have a return value, a way to express such methods is leaving out the result type and the equals sign, following the method with a block enclosed in curly braces. In this form, the method looks like a procedure, a method that is executed only for its side effects.
对于没有返回值的方法,表达这种方法的一种方法是省略结果类型和等号,在方法后面加上一个用花括号括起来的块。在这种形式中,方法看起来像一个过程,一个只为它的副作用而执行的方法。
回答by jos
One thing : imagine the last statement of a method that should return Unit does not return Unit. Using the non-equal syntax is then very convenient, I hope this will not be deprecated as I see several use case for it
一件事:想象一下应该返回 Unit 的方法的最后一条语句不返回 Unit。使用 non-equal 语法非常方便,我希望这不会被弃用,因为我看到了几个用例
回答by BeepDog
As time as progressed the default style has changed, and this has been mentioned in many of the comments to answers, it is recommended in the official style guideto use the =syntax for function declarations.
随着时间的推移,默认样式发生了变化,这已在许多答案的评论中提到,官方样式指南中建议使用=函数声明的语法。

