Is it possible to define unary operators that act from the right?
For example, typically
! is placed on the right for factorial like
Specifically, I’d like to define some operation like
xʳ or alternatively
If that’s not possible, how can I define it in the same way as
- are defined so that
+1 yields 1 and
-1 yields -1 value without using parenthesis like
-(1). How can I define the function
ʳ(x) = foo(x) so that I can use it without parenthesis like
ʳx, preferably with a right-handedness so that I can write
The only unary operators in Julia are
<: >: + - ! ~ ¬ √ ∛ ∜, all of which are parsed as acting from the left. Edit: plus
.', of course, which are postfix operators.
You can’t define arbitrary new symbols as unary operators, only use/extend/redefine those that are already parsed that way. (Nor can you define arbitrary symbols to be binary operators, but this is less of a limitation since there are many more Unicode binary operators to choose from, particularly in 0.7 now that you can add operator suffixes/decorators via #22089.)
And superscripts, in particular, are too useful as part of identifiers to give them up for operators. But note that in 0.7 you can add superscripts to operators, e.g. you can define an operator
Note for those finding this later:
To overload the postfix operator
', you can import
LinearAlgebra, and overload the
adjoint function. It appears that the postfix operator
' always points to whatever is the definition of
adjoint, including any overloads.
For example, this approach is used in the definition of
' as a postfix operator for differentiation in ApproxFun.