Hacker News new | past | comments | ask | show | jobs | submit login

They've been doing that for a long time. Just... slowly.



Slowly is an euphemism.. Lambda Expressions were introduced in c# in 2007, Anonymous delegates exist in c# since 2005, Java introduced closures in 2014...


I didn't mean slowly as a euphemism. It's just slowly. They're extremely methodical about changes to the core language. And, yeah, it's slower than many would like, but they do it right and the language remains stable and reliable.

Anyway, Lambda expressions have been around in, like, Lisp for at least 40 years and in mathematics for, what, 80? So everyone's slow by that account.


I emphasised the “slowly” because c# has the reputation of being “a bad copy of Java”, while apparently the opposite is true given how long it took Java to copy closures and to produce a bad copy of LINQ.


They want to prevent another half-baked implementation of a feature like they did with generics.

C# is much faster and has much more features, but because of that it's also a bit of a mess sometimes. For example regarding closures/anonymous functions it has delegates, lambdas and anonymous (inner) classes which all have overlap but aren't fully the same.

Java only has anonymous inner classes, and a convenient syntax and good support for anonymous inner classes with only 1 method (which they call lamdbas)


Lambdas are a bit more than that, because on standard JVMs they are implemented with specific bytecodes (via an invokedynamic specialization).

The only variant of Java that desugars lambas into anonymous inner classes is Android, because Google.


> Java introduced closures in 2014

Java has had closures since 1997 hasn’t it? Don’t anonymous classes close over local state? Just the syntax was verbose.

Do you mean the lambda syntax? Well that isn’t thue only thing in Java that forms a closure.

‘Closure’ and ‘anonymous function syntax’ are orthogonal.


Java anon classes couldn't capture non-final locals back when they were introduced. So they were closures, but not full-fledged. In contrast, C# anon delegates could capture any local, including writeable ones.


They still can't, captured variables need to be "effectively final" since Java 8, meaning they aren't allowed to be written after the capture.

I think that's reasonable though, capturing mutable state is a recipe for disaster, and if you really want/need to you can wrap it in a properly synchronized container (e.g. AtomicReference in Java)


In most cases, lambdas that capture mutable state effectively take ownership of it, so there's no issue there.


Anonymous inner classes are not proper closures as the other comment explained. And as you noted the syntax is so abhorrent that their use is actively discouraged by the guava developers for example. And all the source code pre-Java 8 that use anonymous inner classes to pass functions around was pretty unreadable.


Why do you think they aren’t proper closures? Because they don’t allow you to mutate captured locals? Neither do closures in Haskell, and neither do lambdas in Java 8, so by that argument Java still doesn’t have closures. Your arguments are inconsistent because you’ve claimed that Java now has closures, even though they still do not meet your definition of closures.

Apart from effective finality and implementation, the semantics of anonymous classes and lambdas is the same!




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: