0

In Scala when we do like:

val fIncr: (x: int) => x+1

My understanding is that here we are defining a function literal. Scala compiler would compile it into a class extending trait Function1 and in run-time it is instantiated and function value is assigned to fIncr.

What happens if I define a similar function as method:

def mIncr(x: Int) = x+1

Is this too compiled to a class?

Edit:

scala> val double = (i: Int) => {
 |     println("I am here")
 |     i * 2
 |   }
 double: Int => Int = $$Lambda$1090/773348080@6ae9b2f0

 scala> double(4)
 I am here
 res22: Int = 8

 scala> val evenFunc: (Int => Boolean) = {
 |                   println("I am here");
 |                    (x => x % 2 == 0)
 |              }
 I am here
 evenFunc: Int => Boolean = $$Lambda$1091/373581540@a7c489a

 scala> double
 res23: Int => Int = $$Lambda$1090/773348080@6ae9b2f0

 scala> evenFunc
 res24: Int => Boolean = $$Lambda$1091/373581540@a7c489a

 scala> evenFunc(10)
 res25: Boolean = true

 scala> def incr(x:Int) = x+1
 incr: (x: Int)Int

 scala> incr
 <console>:13: error: missing argument list for method incr
 Unapplied methods are only converted to functions when a function type is 
 expected.
 You can make this conversion explicit by writing `incr _` or `incr(_)` 
  instead of `incr`.
   incr
   ^

double and evenFunc are function variables and we have assigned function literals to them.But as output shows, when we call double, println statement is also executed.But evenFunc doesn't execute println statement, except when defined. incr is defined with keyword def, so its behaviour is as expected.

Mandroid
  • 6,200
  • 12
  • 64
  • 134

1 Answers1

2

When you do it using def,

def mIncr(x: Int) = x+1

It is method in Scala. Method has not its own identity. It always belongs to a class. But when you do it using val,

val mIncr = (x: Int) => x + 1

It is a function in Scala. Function is a complete object in Scala. It has its own identity. Functions are values in Scala. But Methods are not values.

For example, If you write value, it will give you its information.

scala> def mIncr(x: Int) = x + 1
mIncr: (x: Int)Int

scala> val x = 4
x: Int = 4

scala> x
res0: Int = 4

scala> val inc = (x: Int) => x + 1
inc: Int => Int = <function1>

scala> inc
res1: Int => Int = <function1>

scala> mIncr
<console>:9: error: missing arguments for method mIncr;
follow this method with `_' if you want to treat it as a partially applied function
              mIncr

Notice, when i wrote mIncr, compiler throws an error because it is not a value. Function values used to extend FunctionN trait befor Scala version 2.12 but after release of version 2.12. They don't create anonymous class. Read about it here Functions can be passed around like a parameter of functions. You can not do with methods. When you pass methods, Scala compiler does eta expansion under the hood. Eta expansion means it converts your method into a function value.

Mahesh Chand
  • 3,158
  • 19
  • 37
  • Thanks for the reply. I have a question here: There is an answer here:https://stackoverflow.com/questions/18887264/what-is-the-difference-between-def-and-val-to-define-a-function?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa..by Jatin. Why is that calling even2 doesn't print 'val'. – Mandroid Apr 21 '18 at 05:41
  • The reason is when you defined val, it had printed val at that time. Later it does not do so because value is not being defined again. it just picks the value. – Mahesh Chand Apr 21 '18 at 05:46
  • But when I invoke it with different inputs, correct results are returned.It means body of the function is getting executed. Isn't?So why body is ececuted only partially?println() part is not getting executed, but other part is. – Mandroid Apr 21 '18 at 05:53
  • Yes, at first time when values (x => x % 2 == 0) is getting assigned to it, it executes the body and print the val. But when you use it again, it does not execute the {} block.. it uses the assigned value (x => x % 2 == 0) – Mahesh Chand Apr 21 '18 at 06:16
  • Please see my edit. – Mandroid Apr 21 '18 at 06:29
  • 1
    I think you should put it into a new question rather than editing it. Your own question and it is different. – Mahesh Chand Apr 21 '18 at 06:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169489/discussion-between-mahesh-chand-kandpal-and-mandroid). – Mahesh Chand Apr 21 '18 at 07:02