# Aliases for \`=\` and \`==\`

**URL:** <https://discourse.julialang.org/t/aliases-for-and/60004>\
**Category:** Internals & Design\
**Tags:** proposal\
**Created:** [April 25, 2021, 9:35pm UTC](https://discourse.julialang.org/t/aliases-for-and/60004 "2021-04-25T21:35:44Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![asjir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asjir/32/19899_2.png) [@asjir](https://discourse.julialang.org/u/asjir)\
**Post date:** [May 13, 2021, 5:34pm UTC](https://discourse.julialang.org/t/aliases-for-and/60004/21 "2021-05-13T17:34:36Z")

</div>

I’d be, for once, surprised if writing: `x = -5; x <-4` would return `-4` or `nothing` instead of `true`, because I missed a space there, and there may be existing code that does miss it.

---

<div class="post-metadata">

**Author:** ![D\_A](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@D\_A](https://discourse.julialang.org/u/D_A)\
**Post date:** [May 13, 2021, 9:31pm UTC](https://discourse.julialang.org/t/aliases-for-and/60004/22 "2021-05-13T21:31:11Z")

</div>

These operators exist on R (in both directions) and as I have never seen an assignment with an omitted space, I had never thought about this problem (and I wonder how they handle it on R and if such a mistake is common…). At least, no problem with a hypothetical one-character Unicode operator.

---

<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 13, 2021, 9:33pm UTC](https://discourse.julialang.org/t/aliases-for-and/60004/23 "2021-05-13T21:33:10Z")

</div>

R 4.0.3

```julia
> x = -5
> x<-4
> x
[1] 4
> x < -4
[1] FALSE

```

Julia usually raises a syntax error for ambiguous parses.

---

<div class="post-metadata">

**Author:** ![bradeneliason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bradeneliason/32/29031_2.png) [@bradeneliason](https://discourse.julialang.org/u/bradeneliason)\
**Post date:** [May 20, 2021, 7:06pm UTC](https://discourse.julialang.org/t/aliases-for-and/60004/24 "2021-05-20T19:06:32Z")

</div>

`≟` could be an alias of `==`, but I don’t think I’d ever want to use unicode for that specifically. I could see some value in creating a unicode alias for `copy` and `deepcopy` with assignment. That is, if the alias is `≍` then `b ≍ a` could mean `b = copy(a)` or perhaps `b = deepcopy(a)`. Not sure if I would use that either.

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [May 20, 2021, 8:34pm UTC](https://discourse.julialang.org/t/aliases-for-and/60004/25 "2021-05-20T20:34:23Z")

</div>

The reason why python needs `:=` is simple: python is not an expression-first language, which means that you cannot use assignment statements inside expressions. Hence, an extra syntax for Python is required to bind variables inside expressions.

Julia is born an expression-first language and has no such inconsistency – simply `=` is enough.

---

<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 20, 2021, 9:00pm UTC](https://discourse.julialang.org/t/aliases-for-and/60004/26 "2021-05-20T21:00:42Z")

</div>

Python could have lifted `=` into an expression (and it has done for other statements) but wisely did not.

In Julia, using `=` for assignment makes it possible to confuse

```julia
value = false
if (value == x) # depends on equality

```

with

```julia
value = false
if (value = x) # depends on x

```

Python doesn’t have this problem, which is good because in Python it would be much more severe, since everything behaves like a boolean under `if`. This danger is the reason that people use [Yoda conditions](https://en.wikipedia.org/wiki/Yoda_conditions) in some languages. (Yoda conditions aren’t generally used in Python because they’re not necessary.)

It’s not quite as widespread a problem as it looks since Julia will catch the case without parentheses, but `=` and `==` are nonetheless uncomfortably close and it would be better to have more visual distinction.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 20, 2021, 9:30pm UTC](https://discourse.julialang.org/t/aliases-for-and/60004/27 "2021-05-20T21:30:55Z")

</div>

> [@jzr](#):
>
> In Julia, using `=` for assignment makes it possible to confuse

> **[Returned value of assignment in Julia](https://giordano.github.io/blog/2017-12-02-julia-assignment/)**
>
> Today I realized by chance that in the Julia programming language, like in C/C++, assignment returns the assigned value. To be more precise, assignment returns the right-hand side of the assignment expression. This doesn’t appear to be documented in...

In particular, see the section “Can this feature be a problem in Julia?”

---

<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 20, 2021, 9:33pm UTC](https://discourse.julialang.org/t/aliases-for-and/60004/28 "2021-05-20T21:33:27Z")

</div>

I don’t think it’s so unlikely to have two boolean variables in a condition.

Edit: Nor is it uncommon to want parentheses for clarity.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 20, 2021, 9:33pm UTC](https://discourse.julialang.org/t/aliases-for-and/60004/29 "2021-05-20T21:33:47Z")

</div>

Have you read the post at all?

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [May 20, 2021, 9:35pm UTC](https://discourse.julialang.org/t/aliases-for-and/60004/30 "2021-05-20T21:35:14Z")

</div>

I think this consideration is well covered by Julia.

Julia firstly needs the expressiveness. Unlike giving many syntactic restrictions to programmers by default, Julia needs to consider the convenience of metaprogramming, so that making inconsistent syntax rules for `=` or `:=` is not that appealing in Julia.

If you want the same functionality(i.e., syntactic checking of `=`) as Python’s, I think it should be implemented as a macro and provided by a library.

---

<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 20, 2021, 9:35pm UTC](https://discourse.julialang.org/t/aliases-for-and/60004/31 "2021-05-20T21:35:19Z")

</div>

Yes, it says that writing `if (a == b)` is unlikely (but I disagree), and points out (as I did above) that not using parens avoids the problem.

[Edited to fix “not”]

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [May 20, 2021, 9:38pm UTC](https://discourse.julialang.org/t/aliases-for-and/60004/33 "2021-05-20T21:38:12Z")

</div>

Maybe jzr means not only “top-level” `=` in a condition?

---

<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 20, 2021, 9:43pm UTC](https://discourse.julialang.org/t/aliases-for-and/60004/34 "2021-05-20T21:43:19Z")

</div>

Yeah

```julia
if (a == b) & (c = d) & (e == f); 4 end

```

doesn’t do what I want for example. (If that’s what you mean @thautwarm ?)

It is often helpful to use parentheses to avoid confusion in, say,

```julia
a == b && c == d  

```

to distinguish between

```julia
(a == b) && (c == d)

```

and

```julia
a == (b && c) == d

```

but the parentheses allow a bug to go unnoticed.

---

<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 20, 2021, 10:04pm UTC](https://discourse.julialang.org/t/aliases-for-and/60004/35 "2021-05-20T22:04:10Z")

</div>

On reflection I think @giordano’s point that

> it is kind of useless to test equality with a `Bool` as a condition, since you can use the `Bool` itself.

mostly obviates my earlier link to Yoda conditions: they aren’t usually helpful in Julia since `if true == x` can just write `if x`, and when the left-hand side is a variable Yoda conditions _mostly_ won’t help anyway. One exception is a `const` Bool, like `if (COND = x) || ...` which would error for reassignment of const variable and hence be a useful case for the Yoda condition.

---

<div class="post-metadata">

**Author:** ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)\
**Post date:** [May 20, 2021, 11:44pm UTC](https://discourse.julialang.org/t/aliases-for-and/60004/36 "2021-05-20T23:44:43Z")

</div>

Yes, and that’s what this proposal is meant to address. While getting rid of `=` completely would be a hugely breaking change and require throwing out a lot of old code, asking programmers to get into the habit of using `:=` and `=?` instead of `=` and `==` could prevent a lot of bugs like this.

---

<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 20, 2021, 11:57pm UTC](https://discourse.julialang.org/t/aliases-for-and/60004/37 "2021-05-20T23:57:25Z")

</div>

It seems like your guess is that this situation – `(a == b)` where `a::Bool b::Bool` – is common but that @giordano’s guess is that it isn’t. I think we need some data to resolve that question.

---

<div class="post-metadata">

**Author:** ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)\
**Post date:** [May 21, 2021, 1:48am UTC](https://discourse.julialang.org/t/aliases-for-and/60004/38 "2021-05-21T01:48:59Z")

</div>

I don’t expect cases that completely slip through the compiler without generating an error or some kind of warning are exceptionally common, but I don’t doubt they . I assume they happen from time to time simply because of the size of the community, though. In addition, accidentally using `=` when `==` is meant is an extremely common error, and whenever I read `if (x = y)` in someone else’s code, my brain immediately parses it as the more common `if x == y`. I don’t see a reason to encourage this when we could essentially eliminate these kinds of errors by asking people to use `:=` and `=?` instead.

---

<div class="post-metadata">

**Author:** ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)\
**Post date:** [May 21, 2021, 1:59am UTC](https://discourse.julialang.org/t/aliases-for-and/60004/39 "2021-05-21T01:59:38Z")

</div>

I think the first part could be fixed pretty easily by using something like `=?=` or `=??` to test for identity.

I also think you made my point for me in terms of the low costs of adding this – `\equiv` has already been added to Julia, yet nobody who likes to use `===` instead has suddenly forgotten how to use `===`, gotten very confused by it, or referred to it as “Useless.” It’s just a more legible version of `===`, much like how `:=` is a version of `=` that won’t get confused with `==`. Something like `\triangleq` doesn’t really seem to have any kind of obvious use.

I don’t think anyone would expect `>?` because `>` is already unambiguously a check. People instinctively assume `=` means a test for equality when they start programming because it looks like that’s what it means, and think that assignments like `x = x+1` are unnatural and should just return `false`. Nobody comes to programming thinking `>` will increase the number on the left until it becomes bigger than the number on the right.

---

<div class="post-metadata">

**Author:** ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)\
**Post date:** [May 21, 2021, 2:05am UTC](https://discourse.julialang.org/t/aliases-for-and/60004/40 "2021-05-21T02:05:02Z")

</div>

> [@Jeff\_Emanuel](#):
>
> All this discussion of making Julia look like another language reminds me of the days when people would abuse the C preprocessor to make their C code look a bit more like Pascal. It did no one any good. You still couldn’t write Pascal and feed it to your C compiler, no one conversant with C could read your code, and it slowed learning proper C. Just bite the bullet and learn the language instead of doing a half-baked emulation for another language.

I’m not suggesting people modify their own Julia code to implement this, which would obviously be a pretty dumb idea. I just think implementing `:=` and recommending it over `=` would improve readability and make things make more sense. (Much like how `<-` is much more common than `=` in R, despite `=` existing to keep programmers from getting confused.)

---

<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:** [May 24, 2021, 12:18pm UTC](https://discourse.julialang.org/t/aliases-for-and/60004/41 "2021-05-24T12:18:56Z")

</div>

> [@jzr](#):
>
> I think we need some data to resolve that question.

All languages have these kind of problems (bugs introduced by a typo), unless they are incredibly verbose or full of special cases. It is a trade-off.

A linter may be the best place to catch a problematic `=` in an `if`.

[Previous page](https://discourse.julialang.org/t/aliases-for-and/60004.md?page=1)

[Next page](https://discourse.julialang.org/t/aliases-for-and/60004.md?page=3)
