I would love to be able to use the lambda syntax for single-method interfaces in which the method has multiple parameters. The compiler currently fails when I try this, which I assume is by design based on the documentation:
An alternative syntax for defining anonymous methods is a Lambda expression, which is shorter but cannot provide parameter types and relies on type inference.
For example, the Android API defines interface View.OnClickListener
which has a single method onClick (v: View)
.
Instead of
button.setOnClickListener(new class View.OnClickListener(
onClick := method(v: View) begin
{ code }
end
);
I can type
button.setOnClickListener(-> begin
{ code }
end);
and the compiler also allows
button.setOnClickListener((v: View) -> begin
{ code }
end);
where I can reference parameter v
from within the method.
But in the different case of the Android API’s DialogInterface.OnClickListener
, which defines a single method onClick(v: View; which: Integer)
, I am forced to use the longer syntax instead of
button.setOnClickListener((v: View; which: Integer) -> begin
{ code }
end);
It would be a great feature to be able to use the lambda syntax in these cases as well, as I’ve run across several single-method, multi-parameter interfaces in the Android API.