Marwi
Marwi

Reputation: 274

Multiple consecutive calls of same method without repeating the method name

Is it possible to call a method multiple times in a row and pass in different arguments each time, but without repeating the method name when calling?


For example, in JavaFX, you can add nodes to another node by calling the getChildren.add() method of the parent node each time:

GridPane gridPane = new GridPane();

Button button = new Button();
Label label = new Label();
Checkbox checkbox = new Checkbox();

gridpane.getChildren.add(button);
gridpane.getChildren.add(label);
gridpane.getChildren.add(checkbox);

As you can see, it can get quite repetitive. Is it possible to make the calls without repeating the method name? I would like to know if I could do something like:

GridPane gridPane = new GridPane();

Button button = new Button();
Label label = new Label();
Checkbox checkbox = new Checkbox();

gridPane.getChildren.add {
    button;
    label;
    checkbox;
}

Please note that I am not suggesting the syntax above to be added into the Java language. It was just written to help clarify my point.

Upvotes: 2

Views: 1495

Answers (4)

Ralf Kleberhoff
Ralf Kleberhoff

Reputation: 7290

That's not supported in Java's syntax. The philosophy of Java mainly is to express your program quite explicitly. So, if you want three method calls, you have to write down three method calls (or introduce some kind of loop or use streams).

To explain why such a syntax isn't part of Java, I'll discuss your idea as a potential language extension to Java where something like

m {{
  "foo";
  "bar";
  "foobar";
}};

has the same meaning as

m("foo");
m("bar");
m("foobar");

If asked about such a multi-call feature, I'd definitely vote against its introduction into Java. Why:

  • It doesn't introduce something new, something that wasn't possible before (it's just another way of writing down the same thing as before).
  • It isn't significantly shorter than the classical way.
  • Behind some (IMHO non-intuitive) braces syntax, it hides the fact that there are three method calls. So it reduces one of the most important aspects of software quality: readability.
  • Using the multi-call pattern is less flexible than the classical individual calls. You can't later insert intermediate statements between the calls unless you change back to the classical style.
  • What do you do about the three return values of the method calls? The multi-call language extension must define an answer for this question. Does this m{{...}} multi-call expression return all the values as an array? As a List? Only the last call's return value? None of them? If you use the classical style, it's easy for developers to choose the treatment of the return values. With multi-call, they are bound to only one possible version, the one that became part of the language definition.

I guess that the people responsible for Java's future will at least share some of my doubts, so I bet such a feature will never make it into Java.

Upvotes: 0

enzo
enzo

Reputation: 11496

  1. Use overload:
void m(String value) {
    System.out.println(value);
}

void m(String[] values) {
    for (String value : values) {
        m(value);
    }
}

Usage:

m("foo")  // foo
m("bar")  // bar
m({"foo", "bar"});
// foo
// bar
  1. Use varargs:
void m(String... values) {
    for (String value in values) {
        System.out.println(value);
    }
}

Usage:

m("foo")  // foo
m("bar")  // bar
m("foo", "bar"); 
// foo
// bar

Upvotes: 1

Federico klez Culloca
Federico klez Culloca

Reputation: 27119

There's no syntax similar to your example. The closest you can get for the same effect is either with loops (like in zazz's answer) or with the Stream API.

Depending on whether you care about the return value or not, something like

List.of("foo", "bar", "foobar").forEach(x -> m(x));

or

Stream.of("foo", "bar", "foobar").map(x -> m(x)).collect(Collectors.toList());

Upvotes: 2

zoldxk
zoldxk

Reputation: 1

I don't think it is possible but you can put all the desired string in an array then do a for loop :

String[] arr ={"foo","bar","foobar"};
for(int i=0;i<3:i++){
  m(arr[i]);
}

Upvotes: 2

Related Questions