Collections.sort( list );rather than
list.sort();If 'list' does not actually provide a sort() method. Having extension methods allow "list.sort()" [as suggested in the short example at the link provided] essentially is a lie and obfuscation to the reader of the code.
-- You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to java...@googlegroups.com. To unsubscribe from this group, send email to javaposse+...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/javaposse?hl=.
The reason extension methods are inevitable is because of java's rigid adherence to backwards compatibility. So, if you have to be backwards compatible, and interfaces like java.util.List will NEVER change (even WITH versioning, as lists are routinely transferred across modules - that's Mark Reinhold talking, so consider it canon). Thus, extension methods. That's, again, Mark Reinhold (though I'm paraphrasing. I wasn't running around devoxx with a recorder :P) Technically it seems like a bit of a lie, practically, I really see no problem whatsoever. "It's a lie", you say. Really? Is "list.sort();" a lie? I don't see how it practically will ever be one. It sorts list. I don't think the detail "sort is a method that has been virtually looked up via the object that is being pointed at by the reference variable named 'list'" holds much relevance here.
And, of course, it's trivial for the compiler / editor / style checker / pretty printer / whatever to figure out what's happening and give you a tool to tell these things apart. I would expect the IDEs to render such a call the same way as a static method (example: In eclipse, "sort" would be italicized).
Presumably, imports can gain a scope level and be moved to package- info and module-info in such a way that you can configure a bunch of static and extension imports ONCE per module, and use them everywhere else.
There's some precedent for this; in eclipse, you can configure (IDE-wide, so that's clearly kludgey) standard static imports. In practice, this means you can auto-complete these and if the static import isn't already in the source file, it's added as you autocomplete. Especially with such a feature, lists really WILL gain a sort method. It won't be virtual, but this is mostly irrelevant to me; whenever I am coding and I want to do something to this list, I want to type: "l", "i", "s", "t", ".", "ctrl+space", and pick sort out of the list. If I can't, that sucks. Sucks way more than any concerns about magic going on with extension imports.
inline responses... On Nov 20, 5:48 pm, Jess Holle <je...@ptc.com> wrote:It's a lie in that it claims that list has a sort() method.There is no such claim. dot instead gains an extra possible meaning (pass ref to extension method).
Readability should not rely upon a fancy IDE. I love my IDE, but the text should speak for itself.Why? No, seriously, why? It's almost 2010. Perhaps the dumb terminal model should no longer be held as sacrosanct. The code isn't utter gobbledygook in a dumb terminal - just includes slightly more abstraction. I don't see a fire here.
That's even worse. Then you're left wondering what the heck "sort()" is here and where the heck it came from.Just like x.foo() is a mystery if you don't know the type of x / don't know what x's type is? Code without context is meaningless. Extension methods don't change the equation.
Again, an IDE might come to theThere's no value in the lie that sort() is a method of List. Some ivory tower folk may think OO means every action is a method of the noun on for which it is the principal verb.You ignored my practical reason for this: auto-complete.
[sky will fall down, all hope is lost, go use dynamic language grandstanding omitted]There's no arguing with doomsayers.Yes. I really, really dislike static imports in /most /cases. I'll use them in preference to having my class "implement" an interface class which defines a set of constants (as this is a royal hack), but that's about it. Local clarity is really important -- source characters are cheap.Static import beats new keywords.
On Nov 21, 7:41 pm, Fabrizio Giudici <fabrizio.giud...@tidalwave.it> wrote:[I like list.lookup(Decimator.class).decimate(4);]I don't really get the benefit you're seeing from: list.lookup(Decimator.class).decimate(4); compared to: Decimator.decimate(list, 4);
Both examples are: 1) Unwieldy (in the sense that, when you see it, you get the distinct feeling that you want your editor to fold some of the extraneous stuff away so you can get on with reading your source). 2) Explicitly referencing source, which is an advantage of sorts, though not enough of an improvement over implicit reference to warrant the eyesore. 3) Complicate auto-complete and other 'Uh, how did this API work again?' help.With that syntax I can extend the functionality at willPerhaps you do not understand how extension methods work? The *USE SITE* is where the extensions are defined, so, if you have a custom sort algorithm that you prefer, you can write: import my.customcode.CoolSorter.sort extends java.util.List; instead of importing it from Collections and you'll get your particular flavour of sorting instead. The choices of either the core JDK, or the author of java.util.Collections, or the author of java.util.List, cannot force anything particular on you. In the rare
case you want to mix sort flavours inside the same source file, you'll have to forego extension methods and go back to ProviderClass.methodName(object); instead of object.methodName();
- but as, at this point, the implementation source is clearly no longer a meaningless detail, this would be a good thing. In your proposed syntax, it's always there, even in the vast majority of cases where it's trivial boilerplate junking up your source."I prefer good design"And I prefer music that sounds good. Also, art that looks nice. I like food that tastes good too. Knock it off.
A design which implies that behaviour is *static* rather *dynamic* is a worse design (especially if this is the consequence of a phobia for a few characters more), and this is an objective consideration from OOP best practices, not a subjective one.