OperatorOverloader class abstract interface

A plug-in that gives arithmetic operators a meaning for your own types, such as + on two money amounts.

The evaluator asks it only after its built-in rules (numbers, string concatenation, 'text' * n, 'd' - 3) did not apply; without it such an expression fails with operatorNotSupportedBetweenTypes (EL1030E). Set it on a StandardEvaluationContext; the default, StandardOperatorOverloader, handles nothing.

// `{1} + {2}` concatenates lists.
final class ListConcat implements OperatorOverloader {
  @override
  bool overridesOperation(Operation op, Object? l, Object? r) =>
      op == Operation.add && l is List && r is List;

  @override
  Object? operate(Operation op, Object? l, Object? r) =>
      [...l! as List, ...r! as List];
}

final context = StandardEvaluationContext()
  ..operatorOverloader = ListConcat();
SpelExpressionParser().parseExpression('{1} + {2}').getValue(context);
// [1, 2]

Spring (Java):

StandardEvaluationContext context = new StandardEvaluationContext();
context.setOperatorOverloader(new ListConcatOverloader());

Properties

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
operate(Operation operation, Object? left, Object? right) → Object?
Performs operation; only called after overridesOperation returned true.
overridesOperation(Operation operation, Object? left, Object? right) → bool
Whether this overloader handles operation for left and right.
toString() → String
A string representation of this object.
inherited

Operators

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