Variable number of parameters

Stephen Colebourne scolebourne at joda.org
Mon Dec 5 15:22:16 PST 2011


Consider this looping method (on a DoubleList class for simplicity of
discussion):

interface IndexedBlock {
  void apply(Double value, int index);
}
class DoubleList {
  public void each(IndexedBlock block);
}

To fulfil this functional interface, the writer of the lambda must
specify two parameters:

 list.each( (value, index) -> print("Index " + index + " value " + value) );

But, it would be convenient to be able to only specify the index if
required when writing the lambda:

 list.each( value -> print("Value " + value) );  // this still uses
IndexedBlock!

The example here is based on Fantom, but probably occurs in other
languages. It is important, because it affects the way in which the
APIs are designed - access to the index while looping can be provided
with an additional parameter that is only used when necessary, rather
than needing to provide a separate "eachIndexed()" method.

Of course, such an approach complicates inference, and adds
complexity, taking lambdas further away from inner classes. Its
possible that the EDR allows this, but I don't think it does.

I raise it as I don't recall a discussion here about this (not because
I necessarily think its right for Java).

Stephen


More information about the lambda-dev mailing list