# Make my own alias infix operator

**URL:** <https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631>\
**Category:** New to Julia\
**Tags:** question, infix\
**Created:** [September 7, 2023, 9:22pm UTC](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631 "2023-09-07T21:22:10Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShalokShalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shalokshalom/32/52462_2.png) [@ShalokShalom](https://discourse.julialang.org/u/ShalokShalom)\
**Post date:** [September 7, 2023, 9:22pm UTC](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631/1 "2023-09-07T21:22:10Z")

</div>

Hi there 🙂

I see that custom infix operators are a [long-standing](https://github.com/JuliaLang/julia/issues/39355) [object to discussion](https://github.com/JuliaLang/julia/issues/16985), and I like to ask if there is a way today, how I can alias `∘` to `>>`

As an explanation, my VSCode wont tab complete the \circ and I am already used to \>\> from F#  
But I am unsure if I can make such an alias at all currently, and if so, if its possible to define it as infix operator.

Thanks a lot for your help

---

<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:** [September 7, 2023, 10:06pm UTC](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631/2 "2023-09-07T22:06:21Z")

</div>

> [@ShalokShalom](#):
>
> how I can alias `∘` to `>>`

The biggest reason you couldn’t is both are already used for important functions, function composition and bitshifting respectively, so you’d break something if you could reassign one name to another, which you can’t for constants.

Let’s instead assume you’re talking about an operator that nobody uses; you can make a separate variant of any accepted infix operator by suffixing it like `>>̂` (type `>>\hat<tab>` in REPL), but you cannot define arbitrary text as infix operators. The reason you shouldn’t do that is infix operators have fixed precedence according to their parser, and suffixed variants share precedence with their original. `∘` has different precedence from `>>`, so you don’t want `>>̂` to do what `∘` does.

Example of operator aliasing:

```julia
julia> const >>̂ = ∘
∘ (generic function with 3 methods)

julia> >>̂ # verify successful aliasing
∘ (generic function with 3 methods)

julia> (==(6)∘length)("apple1")
true

julia> (==(6)>>̂length)("apple1")
true

```

But the precedence remains different, so assuming the alias is fully equivalent is incorrect and will cause bugs in operator chains.

```julia
julia> Base.operator_precedence.((:∘, ://, :>>̂)) # larger is prioritized
(12, 13, 14)

julia> Meta.@lower f1 >>̂ f2 ∘ f3
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = f1 >>̂ f2
│ %2 = %1 ∘ f3
└── return %2
))))

julia> Meta.@lower f1 ∘ f2 >>̂ f3
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = f2 >>̂ f3
│ %2 = f1 ∘ %1
└──  

julia> Meta.@lower f1 >>̂ f2 // denom
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = f1 >>̂ f2
│ %2 = %1 // denom
└── return %2
))))

julia> Meta.@lower f1 ∘ f2 // denom
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = f2 // denom
│ %2 = f1 ∘ %1
└── return %2
))))

```

> [@ShalokShalom](#):
>
> my VSCode wont tab complete the \circ

I suggest making a separate thread for this, maybe someone else has ran into and figured out this problem more recently.

---

<div class="post-metadata">

**Author:** ![ShalokShalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shalokshalom/32/52462_2.png) [@ShalokShalom](https://discourse.julialang.org/u/ShalokShalom)\
**Post date:** [September 7, 2023, 10:26pm UTC](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631/3 "2023-09-07T22:26:01Z")

</div>

Thanks a lot for taking the time to answer this 🙂

This reads like its mostly a chore, and really not a straight forward process.  
So I am mostly limited to use symbols, that are either not used at all, or those who have the same precedence level of the original symbol.

I am really not interested in the bitshift operator, but I guess you would still discourage me to replace it. It seems like jumping through hoops, just to get normal composition/piping running.

Will copy/paste `∘` for now ^^

P.S: I opened an issue: [\circ does not expand · Issue #3403 · julia-vscode/julia-vscode · GitHub](https://github.com/julia-vscode/julia-vscode/issues/3403)

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [September 7, 2023, 10:32pm UTC](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631/4 "2023-09-07T22:32:52Z")

</div>

> [@ShalokShalom](#):
>
> my VSCode wont tab complete the \circ

Does the [Fast Unicode Math Characters](https://marketplace.visualstudio.com/items?itemName=GuidoTapia2.unicode-math-vscode) VSCode extension help?

---

<div class="post-metadata">

**Author:** ![ShalokShalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shalokshalom/32/52462_2.png) [@ShalokShalom](https://discourse.julialang.org/u/ShalokShalom)\
**Post date:** [September 7, 2023, 10:45pm UTC](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631/5 "2023-09-07T22:45:54Z")

</div>

Yep, that works!

Thanks

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [September 8, 2023, 3:10am UTC](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631/6 "2023-09-08T03:10:00Z")

</div>

> [@Benny](#):
>
> The biggest reason you couldn’t is both are already used for important functions, function composition and bitshifting respectively, so you’d break something if you could reassign one name to another, which you can’t for constants.

Hunh? You can do `const ∘ = >>`. You just can’t do so after touching Base’s export. This is how shadowing all built in names work.

But your point stands about the mismatched precedences being troublesome.

---

<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:** [September 8, 2023, 3:22am UTC](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631/7 "2023-09-08T03:22:02Z")

</div>

You’re right, by “reassign” I was assuming that the exported names `>>, ∘` were already used. But I suppose it’s very possible to not need both function composition and bitshifting, so shadowing one of the exported names is more likely. Was trying to think of a way to distinguish if a name was created in a module or exported from another one, ~~but I can’t recall one.~~ maybe `asymbol in names(amodule; all=true)`.

---

<div class="post-metadata">

**Author:** ![HanD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hand/32/213908_2.png) [@HanD](https://discourse.julialang.org/u/HanD)\
**Post date:** [September 8, 2023, 6:26am UTC](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631/8 "2023-09-08T06:26:18Z")

</div>

From the old [Julia parser](https://raw.githubusercontent.com/JuliaLang/julia/master/src/julia-parser.scm), one can look up new, unused operators recgonized by Julia with a given level of precendence. In your case, here are all the operators with the same precedence as `\circ`:

```julia
(define prec-times (add-dots '(* / ⌿ ÷ % & · · ⋅ ∘ × |\\| ∩ ∧ ⊗ ⊘ ⊙ ⊚ ⊛ ⊠ ⊡ ⊓ ∗ ∙ ∤ ⅋ ≀ ⊼ ⋄ ⋆ ⋇ ⋉ ⋊ ⋋ ⋌ ⋏ ⋒ ⟑ ⦸ ⦼ ⦾ ⦿ ⧶ ⧷ ⨇ ⨰ ⨱ ⨲ ⨳ ⨴ ⨵ ⨶ ⨷ ⨸ ⨻ ⨼ ⨽ ⩀ ⩃ ⩄ ⩋ ⩍ ⩎ ⩑ ⩓ ⩕ ⩘ ⩚ ⩜ ⩞ ⩟ ⩠ ⫛ ⊍ ▷ ⨝ ⟕ ⟖ ⟗ ⨟)))

```

You can pick any one of these and use as a drop-in replacement for `\circ`, e.g., `\odot`:

```julia
julia> const ⊙ = ∘

```

Admmittedly, the list is a bit short on ASCII symbols, so if tab replacement of symbols is generally non-functional in your VSCode setup, then you’re out of luck.

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [September 8, 2023, 6:52am UTC](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631/9 "2023-09-08T06:52:43Z")

</div>

Just a remark: `>>` in F# is forward, i.e., left-to-right, composition, whereas `∘` adheres to the mathematical convention of right-to-left. Guess that making it an alias would be very confusing at best.  
(It’s fine to define it accordingly though, e.g., `>>(funs...) = ∘(reverse(funs)...)`, but maybe using another symbol due to shadowing `Base.:>>` and different precedence as mentioned by others already).

---

<div class="post-metadata">

**Author:** ![ShalokShalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shalokshalom/32/52462_2.png) [@ShalokShalom](https://discourse.julialang.org/u/ShalokShalom)\
**Post date:** [September 8, 2023, 1:10pm UTC](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631/10 "2023-09-08T13:10:15Z")

</div>

To me, bit shifting sounds like something imperative, and that I am not quite so fond of.

@HanD Oh, thanks for making the effort to present that so orderly to me.  
I generally like to avoid tab completions.

Not because of myself, first and foremost, but because I like to have a symbol that I can show to colleagues, without getting looked at from the corner of the eye, with a raised eyebrow, or two. 😃

@bertschi Thanks - yeah, and F# is using `<<` instead of `∘`.  
They generally recommend |\> and \>\>, since this is more how you read it.

I ultimately found (more than) what I wanted [in this suggestion](https://discourse.julialang.org/t/fixing-the-piping-chaining-issue-rev-3), and really hope it gets accepted. 😃

---

<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:** [September 8, 2023, 1:23pm UTC](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631/11 "2023-09-08T13:23:38Z")

</div>

> [@ShalokShalom](#):
>
> bit shifting sounds like something imperative

What does that even mean? How can 1 pure binary operation distinguish an imperative or functional paradigm? Looking it up, F# has bitshifts `>>>` and `<<<`.

---

<div class="post-metadata">

**Author:** ![ShalokShalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shalokshalom/32/52462_2.png) [@ShalokShalom](https://discourse.julialang.org/u/ShalokShalom)\
**Post date:** [September 8, 2023, 1:47pm UTC](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631/12 "2023-09-08T13:47:06Z")

</div>

F# is a multi paradigm language, and I consider everything imperative, that essentially mutates data. And it is, as far as I can tell, mainly used with low level languages?

> <https://stackoverflow.com/questions/3397421/why-bitshift-operators>

---

<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:** [September 8, 2023, 1:51pm UTC](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631/13 "2023-09-08T13:51:27Z")

</div>

> [@ShalokShalom](#):
>
> I consider everything imperative, that essentially mutates data.

Bitshifts work on immutable integers, so this doesn’t seem logical

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 8, 2023, 4:33pm UTC](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631/15 "2023-09-08T16:33:12Z")

</div>

`>>>` and the other bitshift operators are pure functions, so they should be compatible with functional programming, as far as I can tell.

They _are_ low-level, but is that a problem?

---

<div class="post-metadata">

**Author:** ![ShalokShalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shalokshalom/32/52462_2.png) [@ShalokShalom](https://discourse.julialang.org/u/ShalokShalom)\
**Post date:** [September 8, 2023, 5:20pm UTC](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631/16 "2023-09-08T17:20:11Z")

</div>

I assumed, that it is an impure function.

I don’t comprehend, how a shift of a bit is not mutating state?  
Is it, because the underlying data structure is immutable?

It would be not functional on an array, then.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [September 8, 2023, 5:26pm UTC](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631/17 "2023-09-08T17:26:41Z")

</div>

Do you consider multiplying an integer by two to be an impure and mutating operation? Bitshifts are not fundamentally different.

---

<div class="post-metadata">

**Author:** ![ShalokShalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shalokshalom/32/52462_2.png) [@ShalokShalom](https://discourse.julialang.org/u/ShalokShalom)\
**Post date:** [September 8, 2023, 5:30pm UTC](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631/18 "2023-09-08T17:30:18Z")

</div>

I know very little on the topic of low level operations.  
I am a newbie to programming in general.

And no, I don’t consider multiplying integer mutating state.  
I simply thought, shifting bits meant to actually change them in memory.  
It seems like this is not the case.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 8, 2023, 6:59pm UTC](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631/19 "2023-09-08T18:59:41Z")

</div>

> [@ShalokShalom](#):
>
> I don’t comprehend, how a shift of a bit is not mutating state?  
> Is it, because the underlying data structure is immutable?

It calculates an outout value that is equal to a bit shifted version of the input value. It is similar to multiplying a value by another value.

> [@ShalokShalom](#):
>
> It would be not functional on an array, then.

That would be the same as any other function, if you use it to calculate a new value and then overwrite the old value in an array.

---

<div class="post-metadata">

**Author:** ![ShalokShalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shalokshalom/32/52462_2.png) [@ShalokShalom](https://discourse.julialang.org/u/ShalokShalom)\
**Post date:** [September 8, 2023, 7:49pm UTC](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631/20 "2023-09-08T19:49:12Z")

</div>

So the bit is not really “shifted”, but it’s just a calculation, that does, as if?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [September 8, 2023, 9:29pm UTC](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631/21 "2023-09-08T21:29:02Z")

</div>

Try it out and see how the bits shift.

```julia
julia> 1465 << 1
2930

julia> 1465 << 2
5860

julia> 1465 << 3
11720

julia> string(1465, base = 2, pad = 16)
"0000010110111001"

julia> string(1465 << 1, base = 2, pad = 16)
"0000101101110010"

julia> string(1465 << 2, base = 2, pad = 16)
"0001011011100100"

julia> string(1465 << 3, base = 2, pad = 16)
"0010110111001000"

```

[Next page](https://discourse.julialang.org/t/make-my-own-alias-infix-operator/103631.md?page=2)
