# ★ is not an operator

**URL:** <https://discourse.julialang.org/t/is-not-an-operator/20221>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 29, 2019, 8:47am UTC](https://discourse.julialang.org/t/is-not-an-operator/20221 "2019-01-29T08:47:25Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [January 29, 2019, 8:47am UTC](https://discourse.julialang.org/t/is-not-an-operator/20221/1 "2019-01-29T08:47:25Z")

</div>

Just stumbled about the wish to use ★ as an operator. That does not work

```julia
julia> ★(a, b) = a + b
★ (generic function with 1 method)
julia> 1 ★ 3
ERROR: syntax: extra token "★" after end of expression

```

when looking into [julia/julia-parser.scm at master · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm) it appears that ★ is not found when searched for.  
Was it forgotten?

Anyone knowing what goes on here?  
thanks a lot

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [January 29, 2019, 8:51am UTC](https://discourse.julialang.org/t/is-not-an-operator/20221/2 "2019-01-29T08:51:03Z")

</div>

You’re trying to use a function as an infix operator, that doesn’t work. If you call it normally, it will work as expected.

The reason e.g. `a + b` can be used as an infix is because that is special cased at lowering and actually transforms into `+(a,b)`. See also [the docs](https://docs.julialang.org/en/v1.1/manual/functions/#Operators-Are-Functions-1), Operators Are Functions.

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [January 29, 2019, 9:14am UTC](https://discourse.julialang.org/t/is-not-an-operator/20221/3 "2019-01-29T09:14:29Z")

</div>

I believe the file you linked to is the correct one – the list of symbols which will be parsed as infix multiplication (like `*`) is long, but doesn’t include `★`. I don’t know of a good reason for that, it could be just an oversight?

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

Note that it does include `⋆`, there’s a chance these were judged to be too similar? Both are things you can type:

```julia
help?> ★
"★" can be typed by \bigstar<tab>

help?> ⋆
"⋆" can be typed by \star<tab>

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [January 29, 2019, 9:27am UTC](https://discourse.julialang.org/t/is-not-an-operator/20221/4 "2019-01-29T09:27:47Z")

</div>

If you don’t mind me asking, what operation _should_ ★ be parsed as anyway? In my mind it only makes sense to parse it as infix if there exists some already defined function for it to be called as. In the example by @schlichtanders, ★ is a user defined function, so the parser can’t really know about it beforehand and parse it as infix.

Note that Σ (`\Sigma`) is also possible to type, as are a lot of LaTeX symbols, but that doesn’t necessarily mean they represent something useful out of the box:

```julia
help?> Σ                    
"Σ" can be typed by \Sigma<tab>

search:
Couldn't find Σ
Perhaps you meant [...]

 No documentation found.
 
 Binding Σ does not exist. 

```

See [here](https://docs.julialang.org/en/v1/manual/variables/) for more info:

> In the Julia REPL and several other Julia editing environments, you can type many Unicode math symbols by typing the backslashed LaTeX symbol name followed by tab.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 29, 2019, 9:32am UTC](https://discourse.julialang.org/t/is-not-an-operator/20221/5 "2019-01-29T09:32:24Z")

</div>

> [@Sukera](#):
>
> In the example by @schlichtanders, ★ is a user defined function, so the parser can’t really know about it beforehand and parse it as infix.

`+` is also a “user defined function” with one definition at

> <https://github.com/JuliaLang/julia/blob/59314a9875851ac26316369cb9525f57ba044333/base/int.jl#L53>

As already shown, there are also many symbols that parse as infix without having a meaning already.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [January 29, 2019, 9:41am UTC](https://discourse.julialang.org/t/is-not-an-operator/20221/6 "2019-01-29T09:41:10Z")

</div>

The ★ operator is actually a _unary_ operator in mathematics, and not a _binary_ infix operator

> **[Hodge star operator](https://en.wikipedia.org/wiki/Hodge_star_operator)**
>
> In mathematics, the Hodge star operator or Hodge star is a linear map defined on the exterior algebra of a finite-dimensional oriented vector space endowed with a nondegenerate symmetric bilinear form. Applying the operator to an element of the algebra produces the Hodge dual of the element. This map was introduced by W. V. D. Hodge.
> For example, in an oriented 3-dimensional Euclidean space, an oriented plane can be represented by the exterior product of two basis vectors, and its Hodge dual i...

This is a method which I intend to implement for [Grassmann.jl](https://github.com/chakravala/Grassmann.jl)

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [January 29, 2019, 9:48am UTC](https://discourse.julialang.org/t/is-not-an-operator/20221/7 "2019-01-29T09:48:15Z")

</div>

You are free to define say `Σ(x) = sum(x)`, but can never write `2 Σ 3` because sigma is parsed as an ordinary letter, not an infix operator. It’s pretty neat that lots of symbols are parsed as operators, precisely so that you can later define them as whatever special kind of multiplication your problem needs.

Good point @chakravala that it’s useful for some star-like symbols _not_ to be infix. Ideally you’d like the one you use for hodge to be parsed as unary – are there any such user-definable symbols? E.g. `⊖` works infix like `-` but not alone:

```julia
julia> ⊖3
ERROR: syntax: "⊖" is not a unary operator

```

---

<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:** [January 29, 2019, 9:58am UTC](https://discourse.julialang.org/t/is-not-an-operator/20221/8 "2019-01-29T09:58:50Z")

</div>

> [@chakravala](#):
>
> The ★ operator is actually a _unary_ operator in mathematics, and not a _binary_ infix operator

It has several uses in mathematics and various scientific disciplines. Here’s an example of a binary star operator, from mathematics:

> **[Moyal product](https://en.wikipedia.org/wiki/Moyal_product)**
>
> In mathematics, the Moyal product (after José Enrique Moyal; also called the star product or Weyl–Groenewold product, after Hermann Weyl and Hilbrand J. Groenewold) is an example of a phase-space star product. It is an associative, non-commutative product, ★, on the functions on ℝ2n, equipped with its Poisson bracket (with a generalization to symplectic manifolds, described below). It is a special case of the ★-product of the "algebra of symbols" of a universal enveloping algebra.
> The Moyal pr...

---

<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:** [January 29, 2019, 10:11am UTC](https://discourse.julialang.org/t/is-not-an-operator/20221/9 "2019-01-29T10:11:50Z")

</div>

I think it is fine to wait a bit before defining operators which don’t have a widely accepted arity.

Also, there are plenty of Unicode operators already, so unless one insists on using a particular one, finding a substitute should not be a problem.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [January 29, 2019, 10:26am UTC](https://discourse.julialang.org/t/is-not-an-operator/20221/10 "2019-01-29T10:26:10Z")

</div>

Operators like `+` and `-` can be used as both binary and unary

```nohighlight
julia> +1, -1, 1+1, 1-1
(1, -1, 2, 0)

```

So I don’t see any issue with _-arity_. The main mystery is probably the **precedence** of ★ operation?

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [January 29, 2019, 10:53am UTC](https://discourse.julialang.org/t/is-not-an-operator/20221/11 "2019-01-29T10:53:22Z")

</div>

Ah right, my mistake – binary parsing is no bar to also having unary meaning.

I guess you want precedence slightly above `*`, so that you can write `2★ω`? I see that `∂` is an ordinary letter now, thus parsing `∂V` as `∂(V)` would be a breaking change… not to mention `dω`!

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [January 29, 2019, 10:56am UTC](https://discourse.julialang.org/t/is-not-an-operator/20221/12 "2019-01-29T10:56:38Z")

</div>

Unary and binary `+` are different operators with the same _symbol_, they are definitely not the same operator and get lowered to different functions.

As I said before, `a + b` gets lowered to `+(a,b)`:

```julia
julia> @code_lowered 1 + 2
CodeInfo(
   1 ─ %1 = (Base.add_int)(x, y)
   └── return %1
)

julia> @code_lowered -(1)                                                                                               
CodeInfo(
    1 ─ %1 = (Base.neg_int)(x)
    └── return %1
)

```

EDIT: In the case of `-1`, that’s actually a negative integer literal and doesn’t get lowered to anything:

```julia
julia> @code_lowered -1                                                                                  
ERROR: expression is not a function call or symbol                                                       
Stacktrace:                                                                                               
  [1] error(::String) at ./error.jl:33                                                                     
  [2] top-level scope at [...]

```

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [January 29, 2019, 11:23am UTC](https://discourse.julialang.org/t/is-not-an-operator/20221/13 "2019-01-29T11:23:23Z")

</div>

If you overload `-` then the unary form does use that function, although it seems to be inlined already in what `@code_lowered` shows:

```julia
julia> struct Z z end
julia> Base.:-(z::Z) = println("minus ",z.z)
julia> @code_lowered -Z(3)
CodeInfo(
1 ─ %1 = (Base.getproperty)(z, :z)
│ %2 = (Main.println)("minus ", %1)
└── return %2
)

```

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [January 29, 2019, 11:25am UTC](https://discourse.julialang.org/t/is-not-an-operator/20221/14 "2019-01-29T11:25:16Z")

</div>

> [@Sukera](#):
>
> Unary and binary `+` are different operators with the same _symbol_ , they are definitely not the same operator and get lowered to different functions.

Both the unary and binary operation are called from the same name, `+`

> [@Sukera](#):
>
> As I said before, `a + b` gets lowered to `+(a,b)` :

It may get lowered to a different call, but the `+` name is a single name with both unary and n-ary dispatch.

```Julia
julia> :(+x) |> dump
Expr
  head: Symbol call
  args: Array{Any}((2,))
    1: Symbol +
    2: Symbol x

julia> :(x+y) |> dump
Expr
  head: Symbol call
  args: Array{Any}((3,))
    1: Symbol +
    2: Symbol x
    3: Symbol y

```

As you can see, a unary method is listed as the [3] entry.

```Julia
julia> methods(+)
# 163 methods for generic function "+":
[1] +(x::Bool, z::Complex{Bool}) in Base at complex.jl:277
[2] +(x::Bool, y::Bool) in Base at bool.jl:104
[3] +(x::Bool) in Base at bool.jl:101
...

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [January 29, 2019, 11:50am UTC](https://discourse.julialang.org/t/is-not-an-operator/20221/15 "2019-01-29T11:50:29Z")

</div>

This is a different stage in the process though, long after parsing - my whole point is that this works because `+` can be parsed as infix, which does not apply to `★` as it’s not in the list you linked above.

```julia
julia> f(x,y) = x + y
f (generic function with 1 method)

julia> :(x f y) |> dump
ERROR: syntax: missing comma or ) in argument list

julia> :(x + y) |> dump
Expr
    head: Symbol call
    args: Array{Any}((3,))
        1: Symbol +
        2: Symbol x
        3: Symbol y                                   

```

That’s the difference between the infix operator (which the parser is aware of) and a function masquerading as that same infix operator.

* * *

It seems this topic has changed from “why is ★ not an operator/can’t be used infix” to “let’s add ★ to the list of recognized infix operators” for which I don’t yet see a compelling reason. Is there a case such that both `*` and `★` are needed and none of the already existing unicode characters can be used?

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [January 29, 2019, 12:15pm UTC](https://discourse.julialang.org/t/is-not-an-operator/20221/16 "2019-01-29T12:15:54Z")

</div>

> [@Sukera](#):
>
> Is there a case such that both `*` and `★` are needed and none of the already existing unicode characters can be used?

Yes, there is a case, in my package [Grassmann.jl](https://github.com/chakravala/Grassmann.jl) there is the `*` product for a geometric product and I intend to use `★` for the Hodge star dual operation. These are two entirely separate operations both within the same package.

Whether it’s on the list or not is not that important to me, but I don’t see any reason against it.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 29, 2019, 12:23pm UTC](https://discourse.julialang.org/t/is-not-an-operator/20221/17 "2019-01-29T12:23:19Z")

</div>

> [@schlichtanders](#):
>
> Just stumbled about the wish to use ★ as an operator.

★ is [U+2605](https://www.fileformat.info/info/unicode/char/2605/index.htm), “black star”, in Unicode [category So (Symbol, other)](https://www.fileformat.info/info/unicode/category/So/index.htm).

For the most part (with the exception of some [arrows](https://en.wikipedia.org/wiki/Arrow_(symbol)#Arrows_in_Unicode)), Julia only parses a subset of [category Sm (Symbol, math)](https://www.fileformat.info/info/unicode/category/Sm/index.htm) as infix operators.

In particular, you should look at [U+22C6 (“star operator”)](https://www.fileformat.info/info/unicode/char/22c6/index.htm), typed by tab-completing `\star` in the REPL, which _is_ in category Sm and _is_ parsed as an infix operator:

```julia
julia> ⋆(x,y) = 2x+y
⋆ (generic function with 1 method)

julia> 3 ⋆ 4
10

```

(Unfortunately, in many fonts, `⋆` is shown rather small.)

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [January 29, 2019, 12:49pm UTC](https://discourse.julialang.org/t/is-not-an-operator/20221/18 "2019-01-29T12:49:06Z")

</div>

Thanks, that settles where the list came from.

And answering my own question by scrolling down, the list of unary operators is this: `+ - ! ~ ¬ √ ∛ ∜`, of which `¬` and `∜` aren’t already defined.
