Bound call in Java 7?

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 ++" ...)

+6
java java-7 language-design language-features
source share
6 answers

operator c can be translated into Java using anonymous classes with an initializer:

 Drink margarita = new DrinkBuilder() {{ add("tequila"); add("orange liqueur"); add("lime juice"); withRocks(); withSalt(); }}.drink(); 

the disadvantages of using this idiom are well documented here .

Chained Invocation is an alias for a sequence of methods. This is a well-known idiom and works with any version of Java:

 class Chained { public Chained withFoo() { // ... return this; } public Chained withBar() { // ... return this; } } 

a sentence for JDK 7 enables the chaining method also for the void return type :

 class ChainedJava7 { public void withFoo() { // ... } public void withBar() { // ... } } 
+12
source share
+2
source share

I really like the with instructions of this form, but I prefer their version of VB:

 With testObject .Height = 100 .Text = "Hello, World" .ForeColor = System.Drawing.Color.Green End With 

Because each attribute in the with block should still be preceded . , you know that you are referring to the Object property, and not, say, to a local variable, reducing any namespace conflicts.

If you take your example:

 with (new DrinkBuilder()) { add("tequila"); add("orange liqueur"); add("lime juice"); withRocks(); withSalt(); margarita = drink(); } 

There is no easy way to determine if withSalt() a DrinkBuilder method or a method in a local class. If you only allow the methods of the with -ed object in the with block, then I think that they become much less useful.

+2
source share

I am not a fan of this use with ; I prefer the Python with statement . I agree with you that void should mean void . In the example you provide, if a person really wants to bind method calls, they should simply change the return types to their methods so that they are integer.

+1
source share

Maybe many calls to one object are a sign that some code needs to be moved?

+1
source share

Joshua Bloch in Effective Java . Point # 2 strongly recommends using Builder when you have a constructor with many arguments. One reason is that it can be written to ensure that the constructed object is always in a consistent state. He also avoids creating complex "telescopic constructors" in the class of constructed objects. One more thing: if you want the embedded object to be immutable (for example, to ensure thread safety), it cannot have customization methods.

+1
source share

All Articles