I just read the Java 7 preview presentation (pdf) , and there was a slide on the Chained Invocation. Here is an example used in a slide:
// Construction with setters DrinkBuilder margarita = new DrinkBuilder(); margarita.add("tequila"); margarita.add("orange liqueur"); margarita.add("lime juice"); margarita.withRocks(); margarita.withSalt(); Drink drink = margarita.drink(); // Construction with chained invocation Drink margarita = new DrinkBuilder() .add("tequila") .add("orange liqueur") .add("lime juice") .withRocks() .withSalt() .drink();
And I have mixed feelings about this. No need to bind too many method calls in a single statement. Writing margarita.this() and margarita.that() , on the other hand, is also not very convenient.
Now I come to Java from the world of Delphi. And in Delphi there is a language construct with. This is cherished by few and hated by many (or vice versa?). I find with more elegant than the idea of ββa chain call (which, I believe, works based on the void method, which returns a reference to the object it was called on - and this is the part that I don't like, since void should not return anything )
I would appreciate Java using the with language, so the sample code can be written like this:
Drink margarita = null; with (new DrinkBuilder()) { add("tequila"); add("orange liqueur"); add("lime juice"); withRocks(); withSalt(); margarita = drink(); }
Am I the only one who would prefer this solution for a chained call? Does anyone else think that with can be a useful extension to the Java language? (Reminds me of someone question about the need for "Java ++" ...)
java java-7 language-design language-features
Peter PerhΓ‘Δ
source share