我应该使用 Unit 还是省略我的 scala 方法的返回类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9461880/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:55:42  来源:igfitidea点击:

Should I use Unit or leave out the return type for my scala method?

scalavoidreturn-type

提问by balteo

I am not sure what the difference is between specifying Unit as the return type of my scala method or leaving out the return type altogether. What is the difference?

我不确定将 Unit 指定为我的 scala 方法的返回类型或完全省略返回类型有什么区别。有什么区别?

Can anyone please advise?

任何人都可以请指教吗?

回答by Tomasz Nurkiewicz

Implicit Unitreturn type:

隐式Unit返回类型:

def f() {println("ABC")}

Explicit Unitreturn type:

显式Unit返回类型:

def g(): Unit = {println("ABC")}

Return type inferred from the last method expression, still Unitbecause this is the type of println, but confusing:

从最后一个方法表达式推断的返回类型,仍然Unit因为这是 的类型println,但令人困惑:

def h() = println("ABC")

All the methods above are equivalent. I would prefer f()because the lack of =operator after method signature alone is enough for me. Use explicit : Unitwhen you want to extra document the method. The last form is confusing and actually treated as a warning in intellij-idea.

以上方法都是等价的。我更喜欢f()因为=在方法签名之后缺少运算符对我来说就足够了。: Unit当您想要额外记录方法时,请使用显式。最后一种形式令人困惑,实际上被视为intellij-idea 中的警告。

The =operator is crucial. If it is present it means: "please return whatever the last statement returns" in method body. Obviously you cannot use this syntax for abstractmethods. If it is not, Unitis assumed.

=操作是至关重要的。如果存在,则表示:“在方法主体中返回最后一条语句返回的任何内容”。显然,您不能将此语法用于abstract方法。如果不是,Unit则假设。

回答by Heiko Seeberger

The special syntax for procedures (methods returning Unit) was a mistake. Don't use it. It is confusing and dangerous for beginners with a Java/C(++) background. And it is a unnecessary special treatment. Always use the equal sign, with and without type inference (the latter should only be used for private members):

过程(返回Unit 的方法)的特殊语法是错误的。不要使用它。对于具有 Java/C(++) 背景的初学者来说,这是令人困惑和危险的。而且是不必要的特殊处理。总是使用等号,有和没有类型推断(后者应该只用于私有成员):

def foo(): Unit = someCodeReturningUnit()

private def bar() = someCodeReturningUnit()

回答by Daniel C. Sobral

The Scala community is divided about it. On the one hand, not using explicit return types means you can easily forget the =sign, which results in errors that are often annoying to track. On the other hand, Unit-returning methods having a different syntax puts them in a separate category, which pleases some people.

Scala 社区对此意见不一。一方面,不使用显式返回类型意味着您很容易忘记=符号,这会导致通常难以跟踪的错误。另一方面,Unit具有不同语法的 -returning 方法将它们放在一个单独的类别中,这让一些人感到高兴。

Personally, I'd rather this syntax would go away -- the errors resulting from missing =areannoying to track. But it's not even deprecated, so I'd rather take advantage of it than just suffer the problems of its existence.

就我个人而言,我宁愿这种语法消失——由于缺少=导致的错误很难跟踪。但它甚至没有被弃用,所以我宁愿利用它而不是忍受它存在的问题。

So, use whatever you wish. There's going to be people criticizing your choice either way, and people praising it either way.

所以,随心所欲地使用。无论哪种方式,都会有人批评您的选择,也有人会称赞您的选择。