VarargsFunction class final

A wrapper that lets a Dart function accept any number of trailing arguments when an expression calls it, like a Java varargs method (String.join(delimiter, String... elements)).

Dart has no varargs, and an expression may call a function with any number of arguments. Wrap the function: it is called with the first fixedArity arguments one by one, followed by a List of the rest (empty when there are none). Accepted by StandardEvaluationContext.registerFunction, SpelType.staticMethods and SpelType.constructors.

// `#sum` adds any number of values.
context.registerFunction(
  'sum',
  VarargsFunction((List<Object?> numbers) =>
      numbers.fold<num>(0, (a, b) => a + (b! as num))),
);
parser.parseExpression('#sum(1, 2, 3)').getValue(context); // 6
parser.parseExpression('#sum()').getValue(context); // 0

Spring (Java):

public static int sum(int... numbers) { ... }
context.registerFunction("sum",
    MathUtils.class.getMethod("sum", int[].class));
parser.parseExpression("#sum(1, 2, 3)").getValue(context); // 6

Constructors

VarargsFunction(Function function, {int fixedArity = 0})
Wraps function, which takes fixedArity positional parameters followed by one List<Object?> parameter for the rest; a call with fewer than fixedArity arguments does not match.
const

Properties

fixedArity → int
How many leading arguments are passed individually (0 by default: all arguments arrive in the list).
final
function → Function
The wrapped function.
final
hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

accepts(Object function, int count) → bool
Whether function (a Dart Function or a VarargsFunction) can be called with count positional arguments, judged from its type; true when this cannot be told (more than 8 arguments). Resolvers use it to skip functions that do not fit a call.
apply(Object function, List<Object?> arguments) → Object?
Calls function (a Dart Function or a VarargsFunction) with arguments, packing the trailing ones into a list for a VarargsFunction.
convertArguments(Object function, List<Object?> arguments, TypeConverter converter) → List<Object?>
arguments converted with converter to the parameter types of function where they do not fit, like Spring's ReflectionHelper.convertAllArguments; the list is returned as it is when nothing changes: #half(3) with a double parameter gets 3.0, 'abc'.substring('1') gets 1, 'abc'.charAt(1.0) gets 1. Dart has no reflection, so the parameter types are probed: int, double, num, String and bool parameters of the first maxConvertedArity positional parameters convert; an argument that cannot be converted is passed as it is (the call then fails as usual).
minimumArity(Object function) → int?
The smallest number of arguments function accepts, for error messages, or null when unknown (more than 8).

Constants

maxConvertedArity → const int
How many positional parameters convertArguments can probe; a function with more gets its arguments unconverted.