Difference between method and function in Scala

后端 未结 9 2036
温柔的废话
温柔的废话 2020-11-21 07:22

I read Scala Functions (part of Another tour of Scala). In that post he stated:

Methods and functions are not the same thing

9条回答
  •  悲&欢浪女
    2020-11-21 08:05

    Practically, a Scala programmer only needs to know the following three rules to use functions and methods properly:

    • Methods defined by def and function literals defined by => are functions. It is defined in page 143, Chapter 8 in the book of Programming in Scala, 4th edition.
    • Function values are objects that can be passed around as any values. Function literals and partially applied functions are function values.
    • You can leave off the underscore of a partially applied function if a function value is required at a point in the code. For example: someNumber.foreach(println)

    After four editions of Programming in Scala, it is still an issue for people to differentiate the two important concepts: function and function value because all editions don't give a clear explanation. The language specification is too complicated. I found the above rules are simple and accurate.

提交回复
热议问题