242242 end
243243end
244244
245+ """
246+ Find an operator function by its name in the OperatorEnum, considering the arity.
247+ Throws appropriate errors for ambiguous or missing matches.
248+ """
249+ @unstable function _find_operator_by_name (func_symbol, args, operators)
250+ function_name_offset = 1
251+ degree = length (args) - function_name_offset
252+
253+ matches = Tuple{Function,Int}[]
254+
255+ for arity in 1 : length (operators. ops)
256+ for op in operators. ops[arity]
257+ if nameof (op) == func_symbol
258+ push! (matches, (op, arity))
259+ end
260+ end
261+ end
262+
263+ if isempty (matches)
264+ throw (ArgumentError (
265+ " Tried to interpolate function `$(func_symbol) ` but failed. " *
266+ " Function not found in operators."
267+ ))
268+ end
269+
270+ arity_matches = filter (m -> m[2 ] == degree, matches)
271+
272+ if length (arity_matches) > 1
273+ ops_str = join ([string (m[1 ]) for m in arity_matches], " , " )
274+ throw (ArgumentError (
275+ " Ambiguous operator `$(func_symbol) ` with arity $(degree) . " *
276+ " Multiple matches found: $(ops_str) "
277+ ))
278+ elseif length (arity_matches) == 0
279+ available_arities = [m[2 ] for m in matches]
280+ throw (ArgumentError (
281+ " Operator `$(func_symbol) ` found but not with arity $(degree) . " *
282+ " Available arities: $(available_arities) "
283+ ))
284+ end
285+
286+ return arity_matches[1 ][1 ]:: Function
287+ end
288+
245289""" An empty module for evaluation without collisions."""
246290module EmptyModule end
247291
@@ -264,9 +308,8 @@ module EmptyModule end
264308 func = try
265309 Core. eval (EmptyModule, first (ex. args))
266310 catch
267- throw (
268- ArgumentError (" Tried to interpolate function `$(first (ex. args)) ` but failed." ),
269- )
311+ # Try to find the function in operators by name
312+ _find_operator_by_name (first (ex. args), args, operators)
270313 end :: Function
271314 return _parse_expression (
272315 func, args, operators, variable_names, N, E, evaluate_on; kws...
0 commit comments