Class OverloadedBinaryFunction

java.lang.Object
com.singularsys.jep.functions.PostfixMathCommand
com.singularsys.jep.functions.BinaryFunction
com.singularsys.jep.misc.overloadedfunctions.OverloadedBinaryFunction
All Implemented Interfaces:
JepComponent, PostfixMathCommandI, Serializable

public class OverloadedBinaryFunction extends BinaryFunction implements JepComponent
Allows function to be overloaded with two candidate functions and a predicate that tests the values of the arguments to see which function should be applied. For example if we have a custom value type
        class MyVal {
                double val;

                public MyVal(double val) {
                        super();
                        this.val = val;
                }

                MyVal add(MyVal arg) {
                        return new MyVal(val+arg.val);
                }
        }
 
The addition operator could be overload with a custom function that uses the MyVal.add(MyVal) method.
        var fun1 = new OverloadedBinaryFunction(
                BinaryFunction.instanceOf(MyVal.class, (x,y) -> x.add(y) ),
                new Add(),
                (l,r) -> l instanceof MyVal && r instanceof MyVal );
        jep.getOperatorTable().getAdd().setPFMC(fun1);
        jep.reinitializeComponents();
 

In the constructor for the OverloadedFunction the first argument is our new function created by using a lambda expression and the static method BinaryFunction#instanceOf(Class,BiFunctionWithException). The second argument is the regular multiplication function and the third argument is the predicate that returns true when the values of the lhs and rhs arguments are strings.

Since:
Jep 4.1
See Also: