# Comparing symbols with == vs ===

**URL:** https://discourse.julialang.org/t/comparing-symbols-with-vs/56569
**Category:** General Usage
**Created:** [March 5, 2021, 3:44pm UTC](https://discourse.julialang.org/t/comparing-symbols-with-vs/56569 "2021-03-05T15:44:59Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [March 5, 2021, 3:44pm UTC](https://discourse.julialang.org/t/comparing-symbols-with-vs/56569/1 "2021-03-05T15:44:59Z")

</div>

The documentation of `Symbol` says

> Symbols are immutable and should be compared using `===`.

Is that for the same reason that it was recommended to use `===` when checking for `missing` or `nothing`?

And following the merge of [#38905](https://github.com/JuliaLang/julia/pull/38905) which fixed the performance [issue](https://github.com/JuliaLang/julia/issues/27681) with `ismissing(...)` and `isnothing(...)`, will it still make a difference to use `===` for symbols?

---

<div class="post-metadata">

### Author: ![GpuCoder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gpucoder/32/24511_2.png) [@GpuCoder](https://discourse.julialang.org/u/GpuCoder)
#### Post date: [May 16, 2021, 11:46pm UTC](https://discourse.julialang.org/t/comparing-symbols-with-vs/56569/2 "2021-05-16T23:46:38Z")

</div>

This fixed one of my problems !!!

Using === instead of ==, that is.

But where did you find this documentation ???

Even knowing the answer I can’t find relevant documentation.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [May 17, 2021, 12:00am UTC](https://discourse.julialang.org/t/comparing-symbols-with-vs/56569/3 "2021-05-17T00:00:20Z")

</div>

Unfortunately, the Julia manual uses a automatic generator of HTML pages which creates a not very good search bar. Searching `===`, `(===)`, or `"==="` gives no results. This is a known problem.

---

<div class="post-metadata">

### Author: ![ranocha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ranocha/32/35588_2.png) [@ranocha](https://discourse.julialang.org/u/ranocha)
#### Post date: [May 17, 2021, 5:20am UTC](https://discourse.julialang.org/t/comparing-symbols-with-vs/56569/4 "2021-05-17T05:20:38Z")

</div>

> [@sijo](#):
>
> Symbols are immutable and should be compared using `===` .

```julia
help?> Symbol
search: Symbol

  Symbol

  The type of object used to represent identifiers in parsed julia code (ASTs). Also often used as a name or
  label to identify an entity (e.g. as a dictionary key). Symbols can be entered using the : quote operator:

[...]

  Symbols are immutable and should be compared using ===. The implementation re-uses the same object for all
  Symbols with the same name, so comparison tends to be efficient (it can just compare pointers).
[...]

```

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [May 17, 2021, 5:22am UTC](https://discourse.julialang.org/t/comparing-symbols-with-vs/56569/5 "2021-05-17T05:22:30Z")

</div>

> [@GpuCoder](#):
>
> This fixed one of my problems !!!

Can you give more information on the particular code where it made a difference? Was it a performance issue?

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [May 17, 2021, 5:57am UTC](https://discourse.julialang.org/t/comparing-symbols-with-vs/56569/6 "2021-05-17T05:57:32Z")

</div>

Julia 1.6:

```julia
julia> x = Union{Missing,Float64}[1.0]
1-element Vector{Union{Missing, Float64}}:
 1.0

julia> function foo(x)
           s = 0.0
           x1 = x[1]
           ismissing(x1) ? s : s + x1
       end
foo (generic function with 1 method)

julia> foo(x)
1.0

julia> @code_warntype foo(x)
Variables
  #self#::Core.Const(foo)
  x::Vector{Union{Missing, Float64}}
  x1::Union{Missing, Float64}
  s::Float64

Body::Union{Missing, Float64}
1 ─ (s = 0.0)
│ (x1 = Base.getindex(x, 1))
│ %3 = Main.ismissing(x1)::Bool
└── goto #3 if not %3
2 ─ return s::Core.Const(0.0)
3 ─ %6 = (s::Core.Const(0.0) + x1)::Union{Missing, Float64}
└── return %6

```

Julia master:

```julia
julia> x = Union{Missing,Float64}[1.0]
1-element Vector{Union{Missing, Float64}}:
 1.0

julia> function foo(x)
           s = 0.0
           x1 = x[1]
           ismissing(x1) ? s : s + x1
       end
foo (generic function with 1 method)

julia> foo(x)
1.0

julia> @code_warntype foo(x)
MethodInstance for foo(::Vector{Union{Missing, Float64}})
  from foo(x) in Main at REPL[37]:1
Arguments
  #self#::Core.Const(foo)
  x::Vector{Union{Missing, Float64}}
Locals
  x1::Union{Missing, Float64}
  s::Float64
Body::Float64
1 ─ (s = 0.0)
│ (x1 = Base.getindex(x, 1))
│ %3 = Main.ismissing(x1)::Bool
└── goto #3 if not %3
2 ─ return s::Core.Const(0.0)
3 ─ %6 = (s::Core.Const(0.0) + x1::Float64)::Float64
└── return %6

```

It is easy to end up with type unstable code by checking for missing with `ismissing` instead of `===`, unless you’re on Julia \>=1.7-DEV.  
(Note the `Body::Union{Missing, Float64}` on 1.6 vs `Body::Float64` on master.)

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [May 17, 2021, 6:08am UTC](https://discourse.julialang.org/t/comparing-symbols-with-vs/56569/7 "2021-05-17T06:08:58Z")

</div>

So it sounds like there were two reasons: type instability, which was fixed, and “comparing pointers is faster than checking equality”

> The implementation re-uses the same object for all Symbols with the same name, so comparison tends to be efficient (it can just compare pointers).

I normally consider interning an implementation detail that shouldn’t be relevant to users. Is that wrong here, or is it possible to automatically convert == into === for Symbols at compile time?

---

<div class="post-metadata">

### Author: ![GpuCoder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gpucoder/32/24511_2.png) [@GpuCoder](https://discourse.julialang.org/u/GpuCoder)
#### Post date: [May 17, 2021, 8:59pm UTC](https://discourse.julialang.org/t/comparing-symbols-with-vs/56569/8 "2021-05-17T20:59:23Z")

</div>

> [@sijo](#):
>
> Can you give more information on the particular code where it made a difference? Was it a performance issue?

I’ll try to explain, but that’s likely to reveal more misunderstandings on my part.

I was trying to compare symbolic expressions produced by the `Symbolics` package.

`==` appears to be produce a new symbolic expression. `===` seems to check for some sort of structural equivalence (which is sometimes, but not always, useful).

Maple has `evalb` which has semantics something like, “No really check if they are equal, don’t just create a new expression capturing the postulate that they are equal”.

I’m momentarily under the impression that `isequals` is like Maple’s `evalb`.
