marcin_koss
marcin_koss

Reputation: 5882

Scala function definitions and usage

Based on below examples of defining functions what is the best use of 2 and 3 in different scenarios?

  1. def sum(x: Int, y: Int): Int = { x+y } This is a function definition with arguments, return type and function body

  2. val sum = (x: Int, y: Int) => { x+y } This seems like an assignment of lambda function to a variable, why return type is never defined here?

  3. val sum: (Int, Int) => Int = (x,y) => { x+y } This is defining a function as a type? I don't understand how this works!

All 3 functions when invoked will yield the same result:

scala> sum(1,2) Int = 3

Upvotes: 1

Views: 1704

Answers (3)

Xiaohe Dong
Xiaohe Dong

Reputation: 5023

this is about the difference between method and function. The useful link to show the difference is http://java.dzone.com/articles/revealing-scala-magician%E2%80%99s

I think in the 3rd way, you can understand it like a type T = (Int, Int) => Int and then do this : val sum: T = (x,y) => { x+y }

another thing is that if remove input arguments, method and function is different.

def sum: Int = { 3 } //call sum, get 3

val sum: () => Int = () => { 3 } //call sum, return a function, need to call sum() to get 3

Upvotes: 0

Jörg W Mittag
Jörg W Mittag

Reputation: 369420

  1. def sum(x: Int, y: Int): Int = { x+y } This is a function definition with arguments, return type and function body

This is not a function definition. This is a method definition. Functions and methods are fundamentally different. Functions are objects, methods are not. (Methods belong to objects.) Methods can be polymorphic, functions can't.

  1. val sum = (x: Int, y: Int) => { x+y } This seems like an assignment of lambda function to a variable, why return type is never defined here?

Are you asking why the type of sum isn't declared or why the return type of the function literal isn't declared? The return type of the function literal isn't declared because there is no way in the syntax to do so. You simply cannot declare the return type of a function literal, it is always inferred. The type of sum isn't declared because it is not necessary: it can be inferred to be the same as the type of the function literal, i.e. Function2[Int, Int, Int].

Think val foo = "Hello".

  1. val sum: (Int, Int) => Int = (x,y) => { x+y } This is defining a function as a type?

No. This is the exact same thing as 2., except that here the type of sum is explicitly declared (as (Int, Int) => Int which is syntactic sugar for Function[Int, Int, Int]) instead of inferred. Since the type of sum is known, you can leave off the types of the function parameters, because they can be inferred from the context.

Think val foo: String = "Hello".

Upvotes: 5

Injun Song
Injun Song

Reputation: 101

In line 2, scala compiler inferences type of lambda function.

val sum = (x: Int, y: Int) => { x + y }
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          function value

x and y are type of Int, thus x + y is a Int type. In this case, type of sum is (Int, Int) => Int.

In line 3, it shows the type of sum - (Int, Int) => Int. sum indicates a function same with line 2.

val sum: (Int, Int) => Int = (x,y) => { x+y }
         ~~~~~~~~~~~~~~~~~   ~~~~~~~~~~~~~~~~
         type                function value

Right side of = is function signature and body. It receives two arguments called x and y. Its body is x + y.

Shortly, line 2 and line 3 are equivalent. Line 2 has no type of function value, but scala can inference its type. Line 3 has type of function value explicitly, and omits argument type of signature of function value.

Upvotes: 0

Related Questions