@@ -34,9 +34,31 @@ function Base.showerror(io::IO, e::MissingOperatorError)
3434 return print (io, e. msg)
3535end
3636
37+ """
38+ declare_operator_alias(op::Function, ::Val{arity})::Function
39+
40+ Define how an internal operator should be matched against user-provided operators in expression trees.
41+
42+ By default, operators match themselves. Override this method to specify that an internal operator
43+ should match a different operator when searching the operator lists in expressions.
44+
45+ For example, to make `safe_sqrt` match `sqrt` user-space:
46+
47+ ```julia
48+ DynamicExpressions.declare_operator_alias(safe_sqrt, Val(1)) = sqrt
49+ ```
50+
51+ Which would allow a user to write `sqrt(x::Expression)`
52+ and have it match the operator `safe_sqrt` stored in the binary operators
53+ of the expression.
54+ """
55+ declare_operator_alias (op:: F , _) where {F<: Function } = op
56+
3757function apply_operator (op:: F , l:: AbstractExpression ) where {F<: Function }
3858 operators = get_operators (l, nothing )
39- op_idx = findfirst (== (op), operators. unaops)
59+ op_idx = findfirst (
60+ == (op), map (Base. Fix2 (declare_operator_alias, Val (1 )), operators. unaops)
61+ )
4062 if op_idx === nothing
4163 throw (
4264 MissingOperatorError (
@@ -56,7 +78,9 @@ function apply_operator(op::F, l, r) where {F<:Function}
5678 r:: AbstractExpression
5779 (get_operators (r, nothing ), r)
5880 end
59- op_idx = findfirst (== (op), operators. binops)
81+ op_idx = findfirst (
82+ == (op), map (Base. Fix2 (declare_operator_alias, Val (2 )), operators. binops)
83+ )
6084 if op_idx === nothing
6185 throw (
6286 MissingOperatorError (
0 commit comments