0

I am learning the difference between methods and functions. I am following this link

http://jim-mcbeath.blogspot.co.uk/2009/05/scala-functions-vs-methods.html

The article says if you compile the following code:

class test {
def m1(x:Int) = x+3
val f1 = (x:Int) => x+3
}

We should get two files 1. test.class 2. test$$anonfun$1.class

But I do not get it. Secondly the example says if we execute the following command in REPL, we will get the below

scala> val f1 = (x:Int) => x+3
f1: (Int) => Int = <function>

But I get only this

scala> val f1 = (x:Int) => x+3
f1: Int => Int = $$Lambda$1549/1290654769@6d5254f3

Is it because we are using a different version? Please help.

2
  • 2
    Yes, earlier versions of Scala (and REPL) will produce the <function1> representation of the result. Commented Jul 13, 2017 at 16:45
  • You should never rely on implementation details like this. For example, for obvious reasons, Scala.js will generate ecactly zero class files, always. Commented Jul 19, 2017 at 18:56

1 Answer 1

2

Scala 2.11 and earlier versions behave as shown in the blog post.

The behavior changed in Scala 2.12. Scala now uses the lambda support that was added to version 8 of the JVM, so it doesn't need to emit the extra .class file. As a result, the .jar files produced by 2.12 are usually a lot smaller.

As a side effect of this, Scala can't override toString anymore, so you see the standard JVM toString output for lambdas.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.