In Java, I am looking for a generic template that means arrays of a given type (say Foo ) will allow the method call of the array instance. Behind the scenes, this will repeat all the Foo instances in the array and invoke the instance method for each of them.
Perhaps some code will demonstrate this point better:
public class Foo{ public Foo(){} public void Method1(){} }
So you have:
Foo foo = new Foo(); foo.Method1();
But you could create some kind of generic template for your custom types, which essentially did such things:
Foo[] foos = new Foo[]{new Foo(),new Foo(), new Foo()}; foos.Method1();
It is essentially syntactic sugar for:
foreach(Foo f : foos){ f.Method1(); }
My motivation is that someone can use varargs to:
someHelper(fooInstance1,fooInstance2).Method1()
Where someHelper() returns Foo[] .
If every call to Method1() returned a value, it would be even better if it were transferred to an array of return values (where ReturnVals.size == Foos.size ).
In the worst case scenario, I would have to write a separate class to achieve this for each type for which I need this work, possibly using interfaces to describe the functionality applicable to individual instances and instance arrays.
Is there any Java magic, Design Template or Generic jiggery-pokery that can elegantly achieve this?
Otherwise, if any languages facilitate this inherently?
I appreciate that this will not work for all scenarios, but I believe it is at the discretion of the programmer.
Thank you very much