6

I have this below interface in Java

public interface DoubleOperator {

    double applyAsDouble(double left, double right);
}

And below implementing it using Lambda -

DoubleOperator [] ops = {
(a, b) -> a + b,
(a, b) -> a - b
};

How we can implement this in Apex . Any suggestions. Like using Inferface in Apex

1 Answer 1

8

Lambdas don't exist in Apex. You can use interfaces, but you still have to write out the entire class:

 public interface DoubleOperator {
   double applyAsDouble(double left, double right);
 }
 public class AddDoubleOperator implements DoubleOperator {
   double applyAsDouble(double left, double right) {
     return left + right;
   }
 }
 public class SubtractDoubleOperator implements DoubleOperator {
   double applyAsDouble(double left, double right) {
     return left - right;
   }
 }
 DoubleOperator[] ops = new DoubleOperator[] {
   new AddDoubleOperator(), new SubtractDoubleOperator()
 };

While it'd be nice to have lambdas, that's just not something we can do today in Apex.

1
  • 5
    Worth also saying Apex doesn't even support anonymous implementations.
    – Phil W
    Feb 13, 2021 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.