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

Check your julia-vscode verson?

It auto updates to the latest version. Which version do i need? There’s no setting that needs to be activated?

1 Like

Assignment is the most frequently used operator, it should be ONE character operator. In contrast to most people in this thread, I found = and == is intuitive to be used as the assignment and comparison operators, respectively.

4 Likes

The nice thing about brains is that they are very good at rewiring themselves. Try writing some code that helps you practice the distinction between = and ==. At first you’ll stumble a lot as you’ve already done, and it’ll feel frustrating, but after getting a good night sleep, your troubles should be behind you and you won’t have to think about this anymore.

7 Likes

It is funny: I never get this wrong in if branches or broadcasted comparisons (.==) or general logic flow but I frequently do it wrong with @test x = 2. Something about the context or something. Guess I need more practice writing tests…

10 Likes

I believe that the decision of having a = and == was an error of the past, but trying to go against it now would be another error, at least for Julia (that values the similarity with Matlab and other languages).

Julia greatly minimizes the problem by only allowing Bool in the if clause. In your typical C/C++ code, lots of equality comparisons are between integers and most integers are allowed in an if clause, so a x = 5 is not an error but a much worse silently bug. The Yoda Conditions were proposed to minimize the effects of the problem (yet, they only help if one side of the comparison is a literal or constant).

You clearly are not alone, I have answered someone with a similar problem in the past. In their case, the problem was that even if the code results in an error, you lose the value of the overwritten variable, what is a bother when you are working with Jupyter and the like.

4 Likes

I don’t know, I think \leftarrow[TAB] is pretty easy to type. :wink:

1 Like

It seems like there’s some confusion about OP’s request. They’re just asking for an alias in Base. Many operators have aliases, they’re just asking for one for ==.

1 Like

Not at all. Just \ will drive me crazy. I always confused it with /. Furthermore, at least three keystrokes (autocomplete considered) are needed to type the left arrow. Maybe you have a better way?

1 Like

I am more interested to see a well designed operator for explicitly carrying out float number comparison.

If isn’t good enough then let’s talk about it (in a separate thread please).

1 Like

I was kidding, hence the winking emoji.

2 Likes

I know Julia is not going to change, and I do not expect it to.
I learned Pascal early on (on punched cards) The assignment operator is :=
I still think ‘becomes equal to’ when writing code

The comparison operator could be ?=
I would read that as ‘query equals’

4 Likes

If it’s been 20 years, how much sleep do I need?

2 Likes

Perhaps alternate back and forth between an hour of programming and an hour of deep, meditative trance. :slight_smile:

One style I have seen to reduce the problem is to flip the sides of the equality test. That often places something that cannot be assigned to and gives an error.

julia> a = 20
20

julia> 0 == a
false

julia> 0 = a
ERROR: syntax: invalid assignment location "0" around REPL[3]:1
8 Likes

Shoot, usually “try turning it off and on again” is enough to fix these things, I’m out of ideas!

2 Likes

To join the bikeshed, I think linter integration into IDEs will be the most value for effort mitigation for this. Maybe JET.jl can handle it - try it out!

Every LanguageServer.jl compatible editor already emits a warning in this case, as demonstrated upthread.

4 Likes

Ah, I think I just scrolled past that. Awesome!