Skip to content

Commit

Permalink
Improve documentation for overloaded operators in SpEL
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Feb 2, 2024
1 parent ae17b11 commit 1e432ff
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ following kinds of expressions cannot be compiled.
* Expressions involving assignment
* Expressions relying on the conversion service
* Expressions using custom resolvers or accessors
* Expressions using overloaded operators
* Expressions using selection or projection

Compilation of additional kinds of expressions may be supported in the future.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ follows.
}
throw new UnsupportedOperationException(
"No overload for operation %s and operands [%s] and [%s]"
.formatted(operation.name(), left, right));
.formatted(operation, left, right));
}
}
----
Expand All @@ -578,7 +578,7 @@ Java::
context.setOperatorOverloader(new ListConcatenation());
// evaluates to a new list: [1, 2, 3, 4, 5]
parser.parseExpression("{1, 2, 3} + {4, 5}").getValue(context, List.class);
parser.parseExpression("{1, 2, 3} + {2 + 2, 5}").getValue(context, List.class);
----
Kotlin::
Expand All @@ -589,8 +589,19 @@ Kotlin::
context.setOperatorOverloader(ListConcatenation())
// evaluates to a new list: [1, 2, 3, 4, 5]
parser.parseExpression("{1, 2, 3} + {4, 5}").getValue(context, List::class.java)
parser.parseExpression("{1, 2, 3} + {2 + 2, 5}").getValue(context, List::class.java)
----
======

[NOTE]
====
An `OperatorOverloader` does not change the default semantics for an operator. For
example, `2 + 2` in the above example still evaluates to `4`.
====

[CAUTION]
====
Any expression that uses an overloaded operator cannot be compiled. See
xref:core/expressions/evaluation.adoc#expressions-compiler-limitations[Compiler Limitations]
for details.
====
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ void overloadingOperators() {
context.setOperatorOverloader(new ListConcatenation());

// evaluates to [1, 2, 3, 4, 5]
List list = parser.parseExpression("{1, 2, 3} + {4, 5}").getValue(context, List.class);
List list = parser.parseExpression("{1, 2, 3} + {2 + 2, 5}").getValue(context, List.class);
assertThat(list).containsExactly(1, 2, 3, 4, 5);
}

Expand Down Expand Up @@ -706,7 +706,7 @@ public Object operate(Operation operation, Object left, Object right) {
}
throw new UnsupportedOperationException(
"No overload for operation %s and operands [%s] and [%s]"
.formatted(operation.name(), left, right));
.formatted(operation, left, right));
}
}

Expand Down

0 comments on commit 1e432ff

Please sign in to comment.