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 takesfixedAritypositional parameters followed by oneList<Object?>parameter for the rest; a call with fewer thanfixedArityarguments 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 DartFunctionor a VarargsFunction) can be called withcountpositional arguments, judged from its type;truewhen 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 DartFunctionor a VarargsFunction) witharguments, packing the trailing ones into a list for a VarargsFunction. -
convertArguments(
Object function, List< Object?> arguments, TypeConverter converter) → List<Object?> -
argumentsconverted withconverterto the parameter types offunctionwhere they do not fit, like Spring'sReflectionHelper.convertAllArguments; the list is returned as it is when nothing changes:#half(3)with adoubleparameter gets3.0,'abc'.substring('1')gets1,'abc'.charAt(1.0)gets1. Dart has no reflection, so the parameter types are probed:int,double,num,Stringandboolparameters 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
functionaccepts, for error messages, ornullwhen unknown (more than 8).
Constants
- maxConvertedArity → const int
- How many positional parameters convertArguments can probe; a function with more gets its arguments unconverted.