# Brackets as an operator

**URL:** <https://discourse.julialang.org/t/brackets-as-an-operator/47305>\
**Category:** General Usage\
**Created:** [September 26, 2020, 10:08am UTC](https://discourse.julialang.org/t/brackets-as-an-operator/47305 "2020-09-26T10:08:14Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [September 26, 2020, 10:08am UTC](https://discourse.julialang.org/t/brackets-as-an-operator/47305/1 "2020-09-26T10:08:14Z")

</div>

Is there a way to define an operator not as a symbol, but as brackets? An example: a commutator

```julia
[a::MyType, b::Mytype] = a*b - b*a

```

---

<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 26, 2020, 10:19am UTC](https://discourse.julialang.org/t/brackets-as-an-operator/47305/2 "2020-09-26T10:19:01Z")

</div>

That syntax is already taken, `[a, b]` is a vector.

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [September 26, 2020, 11:00am UTC](https://discourse.julialang.org/t/brackets-as-an-operator/47305/3 "2020-09-26T11:00:47Z")

</div>

You can overload `MyType[a, b]`. I think it is less problematic than your syntax though the julia may internally use both syntax and get unintended side effects or even crash.

```julia
julia> struct Commutator end

julia> Base.getindex(::Type{Commutator}, a, b) = a*b - b*a

julia> Commutator[1, 2]
0

julia> Commutator[1.0, 2.0]
0.0

```

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [September 26, 2020, 11:09am UTC](https://discourse.julialang.org/t/brackets-as-an-operator/47305/4 "2020-09-26T11:09:58Z")

</div>

There other brackets

```julia
{a,b}= a*b-b*a

```

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 26, 2020, 11:10am UTC](https://discourse.julialang.org/t/brackets-as-an-operator/47305/5 "2020-09-26T11:10:09Z")

</div>

> [@Mikhail\_Kagalenko](#):
>
> Is there a way to define an operator not as a symbol, but as brackets?

With a macro, yes. Any valid syntax can be transformed into any other valid syntax. E.g. you could create `@comm [a, b]`. But it’s worth asking, very seriously, whether you get anything out of this transformation beyond “something cool”. That being said, this could be a good and manageably sized exercise to learn metaprogamming.

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [September 26, 2020, 11:18am UTC](https://discourse.julialang.org/t/brackets-as-an-operator/47305/6 "2020-09-26T11:18:49Z")

</div>

Can it be defined without calling a macro? You don’t resort to macro with overpoaded operators.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [September 26, 2020, 11:20am UTC](https://discourse.julialang.org/t/brackets-as-an-operator/47305/7 "2020-09-26T11:20:53Z")

</div>

You can do this, although it’s probably a terrible idea:

```julia
julia> struct MyType data end

julia> Base.vect(x::MyType, y::MyType) = "commutator of $x and $y, maybe"

julia> [MyType(1)]
1-element Array{MyType,1}:
 MyType(1)

julia> [MyType(1), MyType(2)]
"commutator of MyType(1) and MyType(2), maybe"

julia> [MyType(1), MyType(2), MyType(3)]
3-element Array{MyType,1}:
 MyType(1)
 MyType(2)
 MyType(3)

```

Since you own `MyType`, this shouldn’t break other code (the way that doing this for Matrix might). But it will confuse anyone trying to read it.

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [September 26, 2020, 11:40am UTC](https://discourse.julialang.org/t/brackets-as-an-operator/47305/8 "2020-09-26T11:40:09Z")

</div>

That is overloading existing syntax, can it be done with other kind of brackets, like `{}`?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 26, 2020, 11:55am UTC](https://discourse.julialang.org/t/brackets-as-an-operator/47305/9 "2020-09-26T11:55:42Z")

</div>

No, `{}`s are OK in the AST so a macro can deal with them, but they don’t lower to a method you could dispatch on.

FWIW an infix operator would be my first choice for anything like this. There are [so many to choose from](https://github.com/JuliaLang/julia/blob/d7b391dc60f9d30a1ca57668895b5900665dae1c/src/julia-parser.scm#L24).

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [September 26, 2020, 12:31pm UTC](https://discourse.julialang.org/t/brackets-as-an-operator/47305/10 "2020-09-26T12:31:15Z")

</div>

Isn’t there a style recommendation to avoid non-ascii characters in source,if possible?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 26, 2020, 12:55pm UTC](https://discourse.julialang.org/t/brackets-as-an-operator/47305/11 "2020-09-26T12:55:24Z")

</div>

Not that I am aware of.

Personally, I think that using Unicode chars is a neat feature of Julia and should be utilized when it makes sense. Which is especially the case for math operators.
