在Scala中def foo = {}和def foo()= {}有什么区别? [英] What is the difference between def foo = {} and def foo() = {} in Scala?

查看:182
本文介绍了在Scala中def foo = {}和def foo()= {}有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 <$ c在给定以下用于在Scala中定义函数的结构后,您能解释一下它们之间的区别吗? $ c> def foo = {} 

vs。

  def foo()= {} 

更新



感谢您的快速回复。这些都很棒。对我来说唯一的问题是:

如果我省略括号,是否还有一种方法可以传递函数呢?这就是我在repl中得到的结果:

  scala> def foo = {} 
foo:单元

scala> def baz()= {}
baz:()单元

scala> def test(arg:()=> Unit)= {arg}
test:(arg:()=> Unit)()=>单元

scala> test(foo)
< console>:10:error:type mismatch;
找到:Unit
required:()=> Unit
test(foo)
^

scala>测试(baz)
res1:()=>单位=< function0>






更新2012-09 -14



以下是我注意到的一些类似问题:


  1. 功能与括号之间的差异

  2. 没有参数的Scala方法


解决方案

如果在定义中包含括号,那么在调用方法时可以选择省略括号。如果您在定义中忽略它们,则在调用方法时不能使用它们。

  scala> def foo(){} 
foo:()单元

scala> def bar {}
bar:单元

scala> foo

scala> ()
< console>:12:error: >

另外,你可以用你的高阶函数做类似的事情:

  scala> ; def baz(f:()=> Unit){} 
baz:(f:()=> Unit)单位

scala> def bat(f:=> Unit){}
bat:(f:=> Unit)Unit

scala> baz(foo)

scala> baz(bar)
< console>:13:error:type mismatch;
找到:Unit
required:()=>单位
baz(bar)
^
scala> bat(foo)

scala>蝙蝠(酒吧)//两个确定

这里 baz 只会使用 foo()而不是 bar 。这是什么使用,我不知道。但它确实表明这些类型是不同的。


Given the following constructs for defining a function in Scala, can you explain what the difference is, and what the implications will be?

def foo = {}

vs.

def foo() = {}

Update

Thanks for the quick responses. These are great. The only question that remains for me is:

If I omit the parenthesis, is there still a way to pass the function around? This is what I get in the repl:

scala> def foo = {}
foo: Unit

scala> def baz() = {}
baz: ()Unit

scala> def test(arg: () => Unit) = { arg }
test: (arg: () => Unit)() => Unit

scala> test(foo)
<console>:10: error: type mismatch;
 found   : Unit
 required: () => Unit
              test(foo)
                   ^

scala> test(baz)
res1: () => Unit = <function0>


Update 2012-09-14

Here are some similar questions I noticed:

  1. Difference between function with parentheses and without
  2. Scala methods with no arguments

解决方案

If you include the parentheses in the definition you can optionally omit them when you call the method. If you omit them in the definition you can't use them when you call the method.

scala> def foo() {}
foo: ()Unit

scala> def bar {}
bar: Unit

scala> foo

scala> bar()
<console>:12: error: Unit does not take parameters
       bar()
          ^

Additionally, you can do something similar with your higher order functions:

scala> def baz(f: () => Unit) {}
baz: (f: () => Unit)Unit

scala> def bat(f: => Unit) {}
bat: (f: => Unit)Unit

scala> baz(foo)    

scala> baz(bar)
<console>:13: error: type mismatch;
 found   : Unit
 required: () => Unit
       baz(bar)
           ^
scala> bat(foo)

scala> bat(bar)  // both ok

Here baz will only take foo() and not bar. What use this is, I don't know. But it does show that the types are distinct.

这篇关于在Scala中def foo = {}和def foo()= {}有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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