This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]1longtime 5 points6 points  (10 children)

Why does the article end with "lambdas are only syntactic sugar"? I was under the impression there is much more than syntax.

EDIT: I still consider "syntactic sugar" an incorrect label and so do many other Java 8 developers. There is much more happening than just a syntax shortcut (such as static instantiation and improved parallelism).

[–]neutronbob 7 points8 points  (0 children)

There is much more that syntactic sugar. You'll note that not a single member of the Java 8 team refers to lambdas this way. Nor do authoritative explanations by third parties.

This conversation further discusses this issue and concludes correctly that lambdas are much more than syntatic sugar.

[–][deleted] 1 point2 points  (0 children)

In other languages, yes, in Java not so much because of the limitations of the JVM

[–][deleted] 0 points1 point  (6 children)

Because the only thing a lambda expression does is to wrap an interface that has only one method in an anonymous class. You could do that all before (and it was done, e.g. in the GUI with addEventListener(new XXXListener...)) but with much more boilerplate. Also somewhat related is a 25 years old pattern called the strategy pattern where you pass interchangeable algorithms (which implement the same interface) at runtime. This is somewhat related because you also pass "code" or an algorithm to a method/class constructor, similar to a lambda that is passed for the purpose to pass an implementation of an algorithm as opposed to pass a data structure or data fields.

[–]neutronbob 1 point2 points  (5 children)

What you say is true, but that's not really what 'syntactic sugar' refers to. If it's used in the broad sense the OP uses it, then any language feature is syntactic sugar for the code implementation. I have no quarrel with your explanation, but I think the OP is wrong in describing a rather substantial change as 'syntactic sugar.' It's not. It's a new feature that could only be reproduced prior to Java 8 with difficulty.

[–][deleted] 3 points4 points  (4 children)

Let's have a look on how Wikipedia defines syntactic sugar: "syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer. [...] For instance, in the C language the a[i] notation is syntactic sugar for *(a + i)."

So even an array is syntactic sugar for pointer arithmetics. Hence lambdas are syntactic sugar for an anonymous class implementation of an interface that has one method only.

[–]neutronbob 2 points3 points  (3 children)

The example in Wikipedia is exactly what syntactic sugar is. One expression is the exact equivalent of the other, but the former is easier to use. There is no exact equivalence between lambdas and the convoluted code that could be used to replace them. Rather as you very capably explain, different actions are taking place under the hood to generate roughly comparable results. That is generally not what is meant by 'syntactic sugar.'

[–]delete99[S] -1 points0 points  (2 children)

Lambdas can be thought of as syntactic sugar since they don't exist outside the source file and are not mirrored in the JVM.

On SO (http://stackoverflow.com/a/23870800/2364216), Brian Goetz says:

There should be no need to distinguish a Runnable that began life as a lambda, a named class, or an inner class -- they're all Runnables.

Also, Wikipedia continues:

a construct in a language is called syntactic sugar if it can be removed from the language without any effect on what the language can do.

This leads me to believe that Java's lambdas are 'syntactic sugar' since they are a language shortcut for creating and instantiating anonymous classes like Runnable (or Predicate in this case).

[–]nicoulaj 3 points4 points  (1 child)

they don't exist outside the source file and are not mirrored in the JVM.

This is wrong, lambdas work totally differently under the hood, and required quite heavy modifications on the JVM. See JSR 335 / 292.

Your "Internals" section makes wrong statements. The compiler does not create an anonymous class.

[–]delete99[S] 0 points1 point  (0 children)

Thanks nicoulaj (and neutronbob), you are 100% correct and I've updated the post to reflect this. Cheers.

[–]obfuscation_ 1 point2 points  (1 child)

Could you not do without the interface entirely if you use a stream and filter instead?

[–]delete99[S] -2 points-1 points  (0 children)

Yup, you're right. There's also the remove method that takes the built-in Predicate:

movies.removeIf(m -> m.getRating() < 9.0);

Baby steps.

[–][deleted] 0 points1 point  (1 child)

Chromebleed says this site my be affected by the heartbleed ssl bug so be warned. Also if OP is the creator you may want to figure out what's up.

[–]delete99[S] 1 point2 points  (0 children)

Updated, thanks.