Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do lambdas work in Scala, are they functions on top of anonymous classes?

Tags:

lambda

scala

The title might be a little confusing so let me elaborate, I've been reading some criticism regarding Scala. It was an email sent to Tyepsafe regarding some deficiencies in Scala from Coda Hale (Yammer's Infrastructure Architect), so to quote:

we stopped seeing lambdas as free and started seeing them as syntactic sugar on top of anonymous classes and thus acquired the same distaste for them as we did anonymous classes.

So, from this, I have a couple of questions regarding how lambdas work in Scala:

  • What is the difference between a free function and a function that is bound to an anonymous class (technically, aren't all functions bound to the main singleton object)?
  • What is the impact on performance of using an anonymous class bound function instead of a free function?
like image 445
Games Brainiac Avatar asked Dec 31 '13 13:12

Games Brainiac


2 Answers

Yes, lambdas are still objects, instances of anonymous classes.

This is how the JVM works, all references are objects. You can have either references or values (primitives) and there's no way around it.

Later versions of Java have MethodHandles. But it's worth noting that MethodHandle is also still just an abstract class - albeit one that the JVM specifically knows how to optimise away at runtime.

Also also worth noting is that the JVM can often perform escape analysis on abstract classes (such as Scala's functions), and optimise these away too.

On top of this, Scala can use any object with an apply method as though it were a Function. In this case, the explicit call to apply is emitted in the bytecode and you're not dealing with anonymous classes any more.

Given all of the above, it's impossible to make a general statement regarding the performance of Scala's function implementation, it depends on your specific code/use case. In general, I wouldn't worry unless you hit a corner case where your profiler pinpoints a problem here (which is very unlikely)

like image 125
Kevin Wright Avatar answered Sep 25 '22 17:09

Kevin Wright


Well, in C for example a function is just a 32 or 64 bit pointer to a place in memory to jump to and the concept of a closure doesn't really apply since you can't declare an anonymous c function. I don't know how the C++ lambdas work, I guess the compiler makes a method and passes the fields you want in the closure along with parameters. Maybe that's what you're looking for. In the JVM you have to wrap your logic in a class so now you have a virtual table of methods, fields, and some methods related to synchronization and the type system.

What is the impact on performance?...I don't know, have you noticed an impact on performance? A lot of that extra Java stuff I described really isn't needed for an anonymous class and might just get optimized out. I imagine there are butterflies that influence the weather more than the extra JVM stuff would effect your software.

like image 38
HahaHortness Avatar answered Sep 21 '22 17:09

HahaHortness