# How to create custom unary operator

**URL:** <https://discourse.julialang.org/t/how-to-create-custom-unary-operator/74375>\
**Category:** New to Julia\
**Created:** [January 11, 2022, 4:58am UTC](https://discourse.julialang.org/t/how-to-create-custom-unary-operator/74375 "2022-01-11T04:58:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HiramTheHero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hiramthehero/32/218391_2.png) [@HiramTheHero](https://discourse.julialang.org/u/HiramTheHero)\
**Post date:** [January 11, 2022, 4:58am UTC](https://discourse.julialang.org/t/how-to-create-custom-unary-operator/74375/1 "2022-01-11T04:58:08Z")

</div>

Hello all.

Is there a way to create custom unary operators in Julia? (Not overload the predefined ones.)

The only place I’ve been able to find such discussion are:

> [@Right-handed unary operator?](https://discourse.julialang.org/t/right-handed-unary-operator/6280):
>
> Is it possible to define unary operators that act from the right? For example, typically ! is placed on the right for factorial like 5!. Specifically, I’d like to define some operation like xˡ and xʳ or alternatively x^l and x^r. If that’s not possible, how can I define it in the same way as + and - are defined so that +1 yields 1 and -1 yields -1 value without using parenthesis like +(1) or -(1). How can I define the function ʳ(x) = foo(x) so that I can use it without parenthesis like ʳx, pr…

and

> <https://github.com/JuliaLang/julia/issues/18139>
>
> Julia has an \[impressive list\](https://github.com/JuliaLang/julia/blob/c200b4cdb…9620b6df369ae3c735cf3af30b6a47f/src/julia-parser.scm) of binary operators, but the unary operators are quite lonely: \`(+ - ! ¬ ~ |\<:| |\>:| √ ∛ ∜)\`, and most of them have existing meanings. Having a lot of operators makes it easier to define nice-looking DSLs. I can't think of too many mathematical unary operators, except maybe DAGGER † (can we have post-fix unary operators?) so perhaps we could grab ~random unicode characters (☼💣❖) and call them unary?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 11, 2022, 9:36am UTC](https://discourse.julialang.org/t/how-to-create-custom-unary-operator/74375/2 "2022-01-11T09:36:01Z")

</div>

The base language doesn’t provide a way to make arbitrary operators. While looking up a way, I found an approach that involved hacking the parser, but it was done for an old version of Julia.

I think the reason arbitrarily named custom operators aren’t a thing in Julia is the hassle of explicitly specifying operator characteristics like arity, precedence, associativity, short-circuiting, etc. Moreover, operators are often wordless like `+, -, *, /`, so their meaning and characteristics depend a lot on users’ familiarity, often from other contexts e.g. other languages, math. Custom ones won’t be familiar to most users, and if everyone was making their own, Julia could easily become unreadable in practice. Call syntax `add(a, add(subtract(x, y), b) )` requires less memorization to understand than `a %@^&% x %|_% y %@^&% b`.

You’re not completely out of luck, though. Besides overloading the existing operators with their fixed characteristics, you can [suffix an operator with primes, subscripts, and superscripts to make a new one](https://docs.julialang.org/en/v1/manual/variables/#man-allowed-variable-names). The new operator inherits the look and characteristics of the original operator, so familiarity is preserved.

---

<div class="post-metadata">

**Author:** ![HiramTheHero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hiramthehero/32/218391_2.png) [@HiramTheHero](https://discourse.julialang.org/u/HiramTheHero)\
**Post date:** [January 11, 2022, 4:50pm UTC](https://discourse.julialang.org/t/how-to-create-custom-unary-operator/74375/3 "2022-01-11T16:50:04Z")

</div>

Thank you for the thorough response and the mentioned alternative, @Benny. You’re awesome!
