163 points · by rshetty · 14y ago · 139 comments
It's much better to have comparator functions for the most common types baked in, and use projection to select a list of fields to use to compare with.
Comparator functions are very easy to get wrong. The example given here - ".sorted((a, b) -> a.getValue() - b.getValue())" - won't work properly when the calculation wraps around.
Except that requires a sorting key for the result. Trivial for dynamically typed languages, but how do you handle the type of that key in a statically typed language? Mandate that the key be a string? Build a limited set of overloads against a dedicated type? And even then, how do you specify the relative constraints of the different values within that key, especially within the limitations of java's type system?
> The example given here - ".sorted((a, b) -> a.getValue() - b.getValue())" - won't work properly when the calculation wraps around.
But that's less an issue of "comparator functions [being] very easy to get wrong" and more an issue of "integer is an asinine type to use as ordering result". Even more so in a statically typed language. Haskell uses a dedicated enumerated type[0] which does not have this issue.
[0] http://hackage.haskell.org/packages/archive/base/latest/doc/...
And I'll stand by comparators being easy to get wrong, particularly when values may be null, polymorphic, etc.
The result of the projection which is associated with each value and used to actually sort the values.
> The idiom I have in mind is how .Net implements "SortBy", "ThenBy" and friends.
I assume you mean OrderBy. I see, instead of having sorting as a single atomic operation it becomes a stateful multi-step transformation of the iterator. I can still see weirdness in it: what are the constraints on the return value of the key selector functions? The MSDN does not list any, but that can't be right: what if the key selector returns a value which can not be sorted itself?
> And I'll stand by comparators being easy to get wrong, particularly when values may be null, polymorphic, etc.
Trivial key extraction does not make that any easier: instead of being hard, hard cases become impossible.
He means the item by which you sort. The "by" in your "SortBy" example.
Haskell handles this just fine. Static types are only a problem, if they are weak and/or your language syntax is cumbersome. Automatic type inferences helps, of course.
"Doctor, it hurts when I move my arm like this!"
"So don't move your arm like that."
We've spent the last...god knows how many decades dealing with wrap-around on (unsigned generally) integers. :|
http://download.oracle.com/otndocs/jcp/lambda-0_5_1-edr2-spe...
I hope I am wrong but I smell a trap here. Some time ago I implemented a solution (for .Net, that is) that employed some more modern techniques ( reflection, recursive lambdas, etc). Unfortunately many of the programmers in charge of maintaining my code were either interns from a local technical school or an outsourced company in India. They couldn't get their heads around the techniques I used. They thrashed my code, I ended with a bad reputation and lost the client. Not something I regret, anyway.
(1) interns should never be allowed to commit code in the master branch without a code review from a senior developer
(2) there are many competent developers in India, or around the world, so if your company needs to outsource, it can do so by being a little careful about who they employ
(3) if the above 2 are not possible, simply look for another job, because life is too short
The fact is that Java is not a functional language and to bolt on functional pieces and then ignore the imperative, OO nature of Java is stupid. Don't get me wrong, I love functional programming, but Java is not and will never be a functional programming language, and developers that hate on programmers who use a language in the manner in which it was intended to be used are the real fools.
It is a poor programmer indeed that writes pathological code (and functional code in Java is pathological) and then blames his team for not understanding it.
http://stackoverflow.com/questions/429962/when-do-you-use-re...
Even in C# for example, I've seen huge systems written in .Net 4.0 without a single lambda being required.
Lambdas are NEVER required, as tons of other syntactic features are never required.
They are nice to have though, because they make the code more concise and reduce bugs but reducing boilerplate and repetition. Working without them when you have them at your disposal is idiotic.
That huge .Net 4.0 systems have been written without a single lambda probably speaks more about the programmers (not experienced enough?) than about lambdas.
Its not about the programmers but the architecture. To be honest, the primary use case for lambda expressions in .net is LINQ and possibly container configuration. If you use NHibernate and do not expose IEnumerable anywhere (which is required across network boundaries and encapsulation boundaries) then you just don't see it anywhere.
An abstract class you don't, but you can only inherit one anyway.
Also, generics fiasco? Generics may not be perfect, but I haven't met a single person who isn't glad that they are in the language. Who has done generics better? C#? Because C#'s type system is baked so heavily into the run time, the CLR actually has a harder time supporting languages with more powerful type systems than does the JVM.
See, for instance, this post: http://olabini.com/blog/2010/07/questioning-the-reality-of-g.... Both Ola, a JRuby developer, and Martin Oderskey, the creatir of Scala, prefer the JVM's type erasure to the reified generics of the CLR.
Haskell is type erased too but it's not commonly noticed due to the nature of the structures commonly used and the strength of the type system. But in an OOP language where inheritance is used significantly, you really do need Reified generics as they make things much easier. I believe Java also made additional compromises to maintain backwards compatibility.
Of course I have no realistic idea how Oracle would ever migrate millions of Java coders to a cleaner language. :(
Java is designed by its existence to be a better C++, nothing much. Nothing changes that base design goal.
But this is a design goal for the late 80's and early 90's.
The fate of most Java-only programmers, which are uncountable today is pitiable to say at the least. Its like Dijkstra said, one must avoid prolonged exposure to bad tools. Because they permanently damage your brain. Oracle will never do away with Java, its their bait to sell their products. Most Java programmers will go the COBOL programmers way- "Employable only at MegaCorps"
But generics have been a fiasco. No collections of primitive types? Come on, that is a major fiasco for code that needs to be memory efficient.
I hope this excludes the education sector. Nobody should ever learn lambdas as being "implementations of a compiler-guessed interface with one non-defaulted method".
Or time travel. :)
Declared properties and unsigned types are relatively small changes. BTW, in JDK8 they have provided limited support for unsigned types, just not at the language level:
That was bugging me too, thanks for the explanation.
My problem is the same was said for implementing Generics with type erasure. Doesn't make it a good feature for anyone starting a new project. I wish when they soiled the language for compatibility reasons that they would make it an "opt-in" feature.
As far as I can tell, interface defaults don't provide much value over compatibility.
They also don't require you have access to the source (anybody can write an extension method on System.Collections.IEnumerable, not just the BCL team). That they work on any type (not just interfaces) also makes them considerably more general than default implementations.
I also find the idea of an interface carrying implementation a little weird, the abstract class/interface line gets a lot blurrier.
That said, lambda support that only worked on new code would be pretty lackluster; so it's good some thought has gone into getting them into legacy code.
The required exception handling can go next because it doesn't make sense if you version and add new exceptions. Your complier required exception handling was just thrown out.
I also wish that default mechanism could be extended to existing classes as well so we could add methods to classes because the real question is what are the API changes to collection, File, etc with respect to the addition of lambdas. The places where Java has fallen short in the past is the API (Collection.join? Collection.map, Collection.reduce, new File( String... paths), etc).
If there are competing lambda interfaces, does the system fail at compile-time?
I cant stand these "my language is better than your language" debates. Use whatever one makes sense for the job (or, in the case of the enterprise, you can hire enough of).
Virtually every other modern programming language has a lambda, it's good to see Java catch up. This isn't about Java versus any particular language.
It is a pain to write if you don't like having to let the IDE do everything for you, though.
C# lambda expression syntax (the =>) are about 4 years old now.