• 1Installation
  • 2HelloWorld Function
  • 3Function with Arguments
  • 4Function with Variadic Arguments
  • 5Partial Application
  • 6Function Composition
  • 7Function Chaining
  • 8Extend R.apex

R.apex

  • Docs
  • Tutorials
Getting started with R.apex

Function with Variadic Arguments

So far so good. What if we need to handle functions that take variadic arguments? Let's extend our AddFunc to allow adding multiple numbers.

public class AddFunc extends Func {
    public AddFunc() {
        super(-1);
    }

    public override Object execN(List args) {
        Integer sum = 0;
        for(Object arg : args) {
            sum += (Integer)arg;
        }
        return sum;
    }
}

Now this time, we specify the length of the function to be -1, which means that it takes any number of arguments. Also we have our execN(List) overridden to get the sum of all the numbers.

By the way, the default length is -1, so if we want a variadic function, we don't even need to call super(-1). In our case, we can simply omit the constructor.

Done