带括号和不带括号的函数的区别 [英] Difference between function with parentheses and without

查看:116
本文介绍了带括号和不带括号的函数的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

可能的重复:
Scala 中的函数与方法
def foo = 有什么区别{} 和 def foo() = {} 在 Scala 中?

在Scala中我们可以定义

def foo():Unit = println ("你好")

def foo:Unit = println ("你好")

我知道它们不一样但是有什么区别,什么时候应该使用哪个?

如果之前有人回答过这个问题,请把我指向那个链接.

解决方案

Scala 2.x 的 0-arity 方法可以定义为带或不带括号 ().这用于向用户表明该方法具有某种副作用(例如打印到标准输出或破坏数据),而不是没有,稍后可以将其实现为 val.

参见 Scala 编程:><块引用>

这种无参数方法在 Scala 中很常见.相比之下,使用空括号定义的方法,例如 def height(): Int,称为空括号方法.推荐的约定是在没有参数时使用无参数方法,并且该方法仅通过读取包含对象的字段来访问可变状态(特别是,它不会更改可变状态).

该约定支持统一访问原则 [...]

总而言之,Scala 鼓励将不带参数且没有副作用的方法定义为无参数方法,即去掉空括号.另一方面,你不应该定义一个没有括号的有副作用的方法,因为那样调用那个方法看起来就像一个字段选择.

术语

关于 0-arity 方法有一些令人困惑的术语,所以我将在这里创建一个表格:

<头>
Scala 编程scala/scala 行话
def foo: Int无参数方法空方法
def foo(): Int空父方法nilary 方法

我说无效方法"听起来很酷,但经常有人说错了,读者也会感到困惑,所以我建议坚持使用无参数方法 vs 空括号方法,除非你在拉取请求中人们已经在使用行话了.

() 在 Scala 2.13 或 3.0 中不再是可选的

伟大的 () 插入中,Martin Odersky 对 Scala 3 进行了更改要求 () 调用用 () 定义的方法.这记录在 Scala 3迁移指南为:

<块引用>

自动应用是调用空方法而不传递空参数列表的语法.

注意:迁移文档中的术语有误.它应该读作:

<块引用>

自动应用是调用 empty-paren(或nilary")方法而不传递空参数列表的语法.

Scala 2.13,遵循 Scala 3.x 并弃用 中的空括号方法的自动应用如果预期类型为 Function0,则 Eta-expand 0-arity 方法.此规则的一个显着例外是 Java 定义的方法.我们可以不使用()继续调用toString等Java方法.

Possible Duplicate:
Functions vs methods in Scala
What is the difference between def foo = {} and def foo() = {} in Scala?

In scala we can define

def foo():Unit = println ("hello")

or

def foo:Unit = println ("hello")

I know they are not the same but what is the difference, and which should be used when?

If this has been answered before please point me to that link.

解决方案

A Scala 2.x method of 0-arity can be defined with or without parentheses (). This is used to signal the user that the method has some kind of side-effect (like printing out to std out or destroying data), as opposed to the one without, which can later be implemented as val.

See Programming in Scala:

Such parameterless methods are quite common in Scala. By contrast, methods defined with empty parentheses, such as def height(): Int, are called empty-paren methods. The recommended convention is to use a parameterless method whenever there are no parameters and the method accesses mutable state only by reading fields of the containing object (in particular, it does not change mutable state).

This convention supports the uniform access principle [...]

To summarize, it is encouraged style in Scala to define methods that take no parameters and have no side effects as parameterless methods, i.e., leaving off the empty parentheses. On the other hand, you should never define a method that has side-effects without parentheses, because then invocations of that method would look like a field selection.

Terminology

There are some confusing terminology around 0-arity methods, so I'll create a table here:

Programming in Scala scala/scala jargon
def foo: Int parameterless methods nullary method
def foo(): Int empty-paren methods nilary method

I sounds cool to say "nullary method", but often people say it wrong and the readers will also be confused, so I suggest sticking with parameterless vs empty-paren methods, unless you're on a pull request where people are already using the jargons.

() is no longer optional in Scala 2.13 or 3.0

In The great () insert, Martin Odersky made change to Scala 3 to require () to call a method defined with (). This is documented in Scala 3 Migration Guide as:

Auto-application is the syntax of calling a nullary method without passing an empty argument list.

Note: Migration document gets the term wrong. It should read as:

Auto-application is the syntax of calling a empty-paren (or "nilary") method without passing an empty argument list.

Scala 2.13, followed Scala 3.x and deprecated the auto application of empty-paren methods in Eta-expand 0-arity method if expected type is Function0. A notable exception to this rule is Java-defined methods. We can continue to call Java methods such as toString without ().

这篇关于带括号和不带括号的函数的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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