# My Brain Is Hard-Wired Against ==; Help Me, Julia

**URL:** <https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532>\
**Category:** New to Julia\
**Tags:** question, syntax\
**Created:** [June 25, 2021, 6:16am UTC](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532 "2021-06-25T06:16:33Z")\
**Posts on this page:** 20\
**Page:** 4

<div class="post-metadata">

**Author:** ![rashid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rashid/32/9398_2.png) [@rashid](https://discourse.julialang.org/u/rashid)\
**Post date:** [June 30, 2021, 9:41am UTC](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532/63 "2021-06-30T09:41:27Z")

</div>

I am not supporting the OP’s suggestion. But its terrifying that this works in julia,

```julia
a = 0; b = true
if (a = b)
    println("a is equal to b")
end # Output: a is equal to b

```

But it might be hard to determine how much of an issue this is, practically.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [June 30, 2021, 12:53pm UTC](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532/64 "2021-06-30T12:53:27Z")

</div>

I was a bit surprised that worked, it’s less terrifying since without parentheses this is an error:

```julia
if a = b

```

so even if you grew up with them needed, as in C, I would stop using them, since they’re not needed and are harmful. This can’t be fixed it seems (or can it since always a bug [waiting to happen]?), as it would be a breaking change, but I highly recommend the linters should block it.

I’m not sure why it’s allowed (only for compatibility with C?), and what the meaning of this is, since it’s not a tuple as I thought, that would be:

```julia
julia> (a = b,)
(a = true,)

unlike:

julia> (a = b)
true

```

---

<div class="post-metadata">

**Author:** ![etienne\_dg](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etienne\_dg](https://discourse.julialang.org/u/etienne_dg)\
**Post date:** [June 30, 2021, 1:09pm UTC](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532/65 "2021-06-30T13:09:00Z")

</div>

In this case, `true` is assigned to the variable `a`. It seems the parenthesis trick julia to no see the assignment. (Indeed, we can call a function with side effects in a `if` statement, it is a bit similar here I think)

---

<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:** [June 30, 2021, 1:29pm UTC](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532/66 "2021-06-30T13:29:56Z")

</div>

> [@rashid](#):
>
> I am not supporting the OP’s suggestion. But its terrifying that this works in julia,
> 
> ```julia
> a = 1; b = true
> if (a = b)
> println("a is equal to b")
> end # Output: a is equal to b
> 
> ```

uhhhh… If you change the code to:

```julia
a = 1; b = true
if a == b
    println("a is equal to b")
end # Output: a is equal to b

```

then you have the same result. So I am not entirely sure what was your point. `==` is not strict about types and `1` and `true` are the same thing for it.

---

<div class="post-metadata">

**Author:** ![rashid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rashid/32/9398_2.png) [@rashid](https://discourse.julialang.org/u/rashid)\
**Post date:** [June 30, 2021, 1:36pm UTC](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532/67 "2021-06-30T13:36:16Z")

</div>

Sorry bad example. Edited to fix it.

---

<div class="post-metadata">

**Author:** ![etienne\_dg](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etienne\_dg](https://discourse.julialang.org/u/etienne_dg)\
**Post date:** [June 30, 2021, 1:41pm UTC](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532/68 "2021-06-30T13:41:38Z")

</div>

After a bit of searching, I think that the `(a=b)` is a compound expression.  
I did not find any documentation of how it should behave in a condition. I personally would expect the same behavior from a compound expression than from a regular expression. Surely, this would be a breaking change, but maybe it goes under undefined behavior (I’m not sure about this point).

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [June 30, 2021, 1:56pm UTC](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532/69 "2021-06-30T13:56:30Z")

</div>

> [@etienne\_dg](#):
>
> I think that the `(a=b)` is a compound expression.

I think it’s just an expression. I proposed disallowing it, but it was closed very quickly:

> <https://github.com/JuliaLang/julia/issues/41422>
>
> \`\`\`
> if (a = b)
> \`\`\`
> 
> is a bug waiting to happen, and serves not good purpose.… I'm not proposing disallowing parenthesis, just in that context, i.e. ok with == what was meant. Longer story:
> 
> https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532/64?u=palli
> 
> This would be a (technically) breaking change. Can it still be done in pre-2.0?

`a=b` is an assignment, and all expressions in Julia (as with some other languages) return a value, so here `a` (that’s same as `b`, just now). It’s only that without parentheses you don’t get access to it, the truth-value.

Most often you get away with this (while a runtime, rather than compile time, error):

```julia
julia> a=1;b=1;
julia> if (a=b)
        print("Equal")
       end
ERROR: TypeError: non-boolean (Int64) used in boolean context

```

it’s only a problem when b (or both) is a Boolean, mitigating the issue. I still think most often an error for any type combination, and would like to see a useful counterexample.

---

<div class="post-metadata">

**Author:** ![rashid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rashid/32/9398_2.png) [@rashid](https://discourse.julialang.org/u/rashid)\
**Post date:** [June 30, 2021, 2:03pm UTC](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532/70 "2021-06-30T14:03:11Z")

</div>

Yes. Its probably not an issue in practice as `b` needs to be Boolean.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [June 30, 2021, 2:09pm UTC](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532/71 "2021-06-30T14:09:13Z")

</div>

Patterns like this are sometimes useful:

```julia
if (flag = test_that_should_only_be_done_once())
    start_something()
end
do_stuff()
if flag
   finish_something()
end

```

---

<div class="post-metadata">

**Author:** ![etienne\_dg](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etienne\_dg](https://discourse.julialang.org/u/etienne_dg)\
**Post date:** [June 30, 2021, 2:11pm UTC](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532/72 "2021-06-30T14:11:14Z")

</div>

> [@Palli](#):
>
> I still think most often an error for any type combination, and would like to see a useful counterexample.

A typical use case is :

```julia
if (result = computation())
   # do something with result
end

```

[Another use case example](https://www.python.org/dev/peps/pep-0572/#a-numeric-example) is given in the PEP report for the python walrus operator. It helps storing a computation needed for the condition to use it inside the if statement.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [June 30, 2021, 2:20pm UTC](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532/73 "2021-06-30T14:20:00Z")

</div>

> [@etienne\_dg](#):
>
> ```julia
> if (result = computation())
> # do something with result
> end
> 
> ```

That pattern is useful in Python and C, but in Julia, the _only_ value that `result` can have inside that if block is `true`, so there’s no need to store it (unless it will also be used outside of the if-statement).

---

<div class="post-metadata">

**Author:** ![rashid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rashid/32/9398_2.png) [@rashid](https://discourse.julialang.org/u/rashid)\
**Post date:** [June 30, 2021, 2:21pm UTC](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532/74 "2021-06-30T14:21:31Z")

</div>

> [@etienne\_dg](#):
>
> A typical use case is :
> 
> ```julia
> if (result = computation())
> # do something with result
> end
> 
> ```

Don’t think this can be used generally as this would only work if `computation()` was a Boolean value. But the example @Per gave seems to be a valid use case.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [June 30, 2021, 2:47pm UTC](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532/75 "2021-06-30T14:47:14Z")

</div>

Is this three-way pattern used enough, in Julia, to outweigh the danger of when = should be == ?

It seems to be the only counterexample (or possibly four-way+ patterns do also exist?). Could this pattern be named, and be in Base: `some_name(condition(), start(), middle(), end())` and those functions could be anonymous, and possibly number 4+?

Just looking at _one_ if individually, it can be:

```julia
flag = test_that_should_only_be_done_once()
if flag

```

and shorter (by number of letters) if `flag` is changed to `f` (while still more vertical space, and variable repeated), for _two_ ifs, there’s redundancy anyway, and with the proposed named pattern the variable would never need to be named.

While loops are also another issue, can also be rewritten.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [June 30, 2021, 3:33pm UTC](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532/76 "2021-06-30T15:33:04Z")

</div>

> [@Palli](#):
>
> Is this three-way pattern used enough, in Julia, to outweigh the danger of when = should be == ?

A mistaken `=` instead of `==` is only a problem if you ignore the advice in the manual that says [“Don’t parenthesize conditions”](https://docs.julialang.org/en/v1/manual/style-guide/#Don't-parenthesize-conditions) _and_ the right-hand side is a `Bool`, so it probably happens fairly rarely.

The advantage of putting the condition on the same line as the `if` keyword is readability.

Whether the tradeoff is worth it is entirely subjective, of course.

---

<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:** [June 30, 2021, 5:05pm UTC](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532/77 "2021-06-30T17:05:31Z")

</div>

My main problem with disallowing assignment in an `if`-condition is _inconsistency_.

When looking at the code, and knowing about the behavior of assignment, I would reason that it _ought to_ work, even if it’s not so useful. One would have to special-case this to disallow it, and that makes the language more inconsistent, strange and surprising.

---

<div class="post-metadata">

**Author:** ![heetbeet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heetbeet/32/20958_2.png) [@heetbeet](https://discourse.julialang.org/u/heetbeet)\
**Post date:** [July 1, 2021, 6:25am UTC](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532/78 "2021-07-01T06:25:43Z")

</div>

Yes, I use this type of assignment all the type, mostly within while loops where I like to mutate the state:

```julia
i=0
while (i = i +1) <= length(lst)
    if dotest(lst[i]) pop!(lst, i) end
end

```

This isn’t the best example but with something like parsing tokens, you can easily have some forward-looking logic within that loop that also mutates your list. Sometimes you even want to mutate the list at a location `<i` then you just have to increase/decrease i if you removed or added an item.

---

<div class="post-metadata">

**Author:** ![blackeneth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blackeneth/32/10353_2.png) [@blackeneth](https://discourse.julialang.org/u/blackeneth)\
**Post date:** [July 2, 2021, 8:19am UTC](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532/79 "2021-07-02T08:19:52Z")

</div>

Heh, I hate ← in R, but I really like := in Pascal.

To each his own!

---

<div class="post-metadata">

**Author:** ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)\
**Post date:** [July 2, 2021, 9:27am UTC](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532/80 "2021-07-02T09:27:42Z")

</div>

I wonder, couldn’t the compiler decide the context if we use **=** for assignments and bool comparisons? One might think that a multiple dispatch language should be the right place for this.

---

<div class="post-metadata">

**Author:** ![blackeneth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blackeneth/32/10353_2.png) [@blackeneth](https://discourse.julialang.org/u/blackeneth)\
**Post date:** [July 2, 2021, 10:06am UTC](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532/81 "2021-07-02T10:06:46Z")

</div>

Some languages do that – the interpretation of = depends on context.

---

<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:** [July 2, 2021, 2:15pm UTC](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532/82 "2021-07-02T14:15:45Z")

</div>

> [@viraltux](#):
>
> I wonder, couldn’t the compiler decide the context if we use **=** for assignments and bool comparisons?

But how would the compiler know whether the user deliberately used assignment, or _intended_ to use comparison, but misspelled it?

> [@viraltux](#):
>
> One might think that a multiple dispatch language should be the right place for this.

Multiple dispatch has to do with the types used, not with the context.

Imho, this would make the language more inconsistent and unpredictable. Trying to get Julia to interpret intent, turning it into a ‘do what I mean language’ would not be a good thing. Especially in cases like these, where it is actually fundamentally _impossible_ to know the intent.

[Previous page](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532.md?page=3)

[Next page](https://discourse.julialang.org/t/my-brain-is-hard-wired-against-help-me-julia/63532.md?page=5)
