What's the difference between using and no using a “=” in Scala defs ?

后端 未结 5 689
情歌与酒
情歌与酒 2021-01-21 11:06

What the difference between the two defs below

def someFun(x:String) { x.length } 

AND

def someFun(x:String) = {          


        
相关标签:
5条回答
  • 2021-01-21 11:36

    The former is

    def someFun(x: String): Unit = {
      x.length
      ()  // return unit
    }
    

    And the latter is

    def someFun(x: String): Int = {
      x.length  // returned
    }
    
    0 讨论(0)
  • 2021-01-21 11:42

    Without the equals it is implicitly typed to return Unit (or "void"): the result of the body is fixed - not inferred - and any would-be return value is discarded.

    That is, def someFun(x:String) { x.length } is equivalent to def someFun(x:String): Unit = { x.length }, neither of which are very useful here because the function causes no side-effects and returns no value.

    The "equals form" without the explicit Unit (or other type) has the return type inferred; in this case that would be def someFun(x:String): Int = { x.length } which is more useful, albeit not very exciting.


    I prefer to specify the return type for all exposed members, which helps to ensure API/contract stability and arguably adds clarity. For "void" methods this is trivial done by using the Procedure form, without the equals, although it is a stylistic debate of which is better - opponents might argue that having the different forms leads to needless questions about such ;-)

    0 讨论(0)
  • 2021-01-21 11:44

    Note that the Scala Style guide always recommend using '=', both in

    • method declaration

    Methods should be declared according to the following pattern:

    def foo(bar: Baz): Bin = expr
    
    • function declaration

    Function types should be declared with a space between the parameter type, the arrow and the return type:

    def foo(f: Int => String) = ...
    def bar(f: (Boolean, Double) => List[String]) = ...
    
    0 讨论(0)
  • 2021-01-21 11:49

    As others already pointed out, the former is a syntactic shortcut for

    def someFun(x:String): Unit = { x.length }
    

    Meaning that the value of x.length is discarded and the function returns Unit (or () if you prefer) instead.

    I'd like to stress out that this is deprecated since Oct 29, 2013 (https://github.com/scala/scala/pull/3076/), but the warning only shows up if you compile with the -Xfuture flag.

    scala -Xfuture -deprecation
    
    scala> def foo {}
    <console>:1: warning: Procedure syntax is deprecated. Convert procedure `foo` to method by adding `: Unit =`.
           def foo {}
    foo: Unit
    

    So you should never use the so-called procedure syntax. Martin Odersky itself pointed this out in his Scala Day 2013 Keynote and it has been discussed in the scala mailing list.

    The syntax is very inconsistent and it's very common for a beginner to hit this issue when learning the language. For this reasons it's very like that it will be removed from the language at some point.

    0 讨论(0)
  • 2021-01-21 11:51

    As per Scala-2.10, using equals sign is preferred. Infact you must use equals sign in call declarations except the definitions returning Unit.

    As such there is no difference but the first one is not recommended anymore and it should not be used.

    0 讨论(0)
提交回复
热议问题