template<typename Fn, typename ReverseLeft, typename ReverseRight>
BinaryNumericFunctionHelper struct
Helper for creating a reversible numeric binary function.
With this helper you don't have to manually match the Value
s you just have to provide functions that take Number
values and return Number
values for:
- the normal function you want to write
- the reverse function for propagating the changed result to the left parameter
- the reverse function for propagating the changed result to the right parameter
Usage (using the deduction guide):
function pow_impl(const CallContext& ctx) -> Value { return minilua::BinaryNumericFunctionHelper{ [](Number lhs, Number rhs) { return std::pow(lhs.as_float(), rhs.as_float()); }, [](Number new_value, Number old_rhs) { return std::pow(new_value.as_float(), 1 / old_rhs.as_float()); }, [](Number new_value, Number old_lhs) { return std::log(new_value.as_float()) / std::log(old_lhs.as_float()); } }(ctx); }
Note the call at the end.
It's also possible to directly use the BinaryNumericFunctionHelper
as the argument to Function
because it is callable with a CallContext
argument.
See also: UnaryNumericFunctionHelper
.