Tasty, nutritious

...part of this complete breakfast
Everything here is my personal opinion. I do not speak for my employer.
March 2015
April 2015

2015-03-22 »

#TIL  about Java "default methods."  What a hack.  I'm without words.  Interfaces can have method implementations now?

http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

What they could have done to achieve the same effect is copy Extension Methods from C#.  Those are really elegant and not hacky at all: https://msdn.microsoft.com/en-us/library/bb383977.aspx

There are many reasons extension methods are nice.  But the main things that appeal to me are:

1. Anybody can add them later without modifying the base interface (so you can create a new package that adds extension methods to the system-provided List or String or anything else).

2. The new extension methods only appear if you import the package that implements them into the caller's namespace.  This turns out to be exactly what you want, because the caller is the one trying to use the new semantics, or who will be surprised if new methods suddenly appear in an old class, respectively.

Extension Methods are sometimes compared to monkeypatching in Ruby and javascript, but that comparison is not correct, because of #2.  If you add extension methods to the string class, the thus-extended objects do not appear extended for anybody you pass them to, so there's no "spooky action at a distance" problem like there is in Ruby.

Those who do not understand X are doomed to reinvent it, poorly.

Why would you follow me on twitter? Use RSS.
apenwarr-on-gmail.com