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

My 2 cents: add spaces before and after the “==”. It’ll make errors more obvious:

if member == "SB" ...etc
if member == "SL" ...etc
if member == "SU" ...etc
if member = "SN" ... etc
if member = "ST" ... etc
if member = "AUTO" ... etc
17 Likes

Turns out there is already a lint for this in the VS code plugin:

image

and hovering over the blue underline shows this message:

31 Likes

it’s like a new category, asking for syntax change 2nd I’ve seen this on this forum. interesting. doubt it will happen. also just something to get used to

You have other options in Julia than inventing a new unicode. isequal is equivalent to == in most cases (NaN, missing, -0.0 are exceptions). Choose the one best in your eyes, to me the last is awesome.

julia> x = 5;

julia> if x == 5
           println("x is five!")
       end
x is five!

julia> isequal(x, 5) && println("x is five!")
x is five!

julia> x == 5 && println("x is five!")
x is five!
7 Likes

@Seif_Shebl my thoughts exactly. Use the function!

This is definitely not going to happen, but I think the ideal syntax is for = to represent boolean equality and for the assignment operator to be either :=, <-, or .

Luckily downvoting is not possible on Discourse. I know that there is a lot of hate for the <- assignment operator in R. :joy:

8 Likes

The mathematician in me agrees with := for assignment and = for boolean, but since (as you say) it will probably never happen (just as the physical flow of electrons will always be negative current in that direction), maybe you can trick your brain into reading == as “is (=) equal to (=)”. Like != is read “not (!) equal to (=)”, and >= is read “greater than or (>) equal to (=)”. Sometimes you just gotta take the placebo pill and hope it works.

8 Likes

I realize I’m not being too helpful, but if you insist on = for equality, you can with:

The net result is that LispSyntax.jl is really an alternative S-expression-like syntax for julia, not an implemention of clojure or lisp.

You’ll get more than you bargained for, a whole new language/syntax (while keeping Julia semantics), and that applies double for APL.jl:

https://news.ycombinator.com/item?id=27462090

For languages with = for equality (I’m really conflicted on that better), then you need an alternative for assignment, and this seems like a valid option, while you need twice the number of keystrokes for the more common operator:

The downside for Lisp/Scheme/Clojure as above is assignment is even longer (not sure how it’s done in APL), e.g. set! for:

shell> julia --lisp
;  _
; |_ _ _ |_ _ |  . _ _
; | (-||||_(_)|__|_)|_)
;-------------------|----------------------------------------------------------

In MathCad 14 I type
: to get the assignment :=
Ctrl = to get the Boolean equal shown as a bold =
= to find the contents of a variable
{ for a local assignment operator ← (used in their programming blocks)

1 Like

Which VS code plugin is that?

It’s the Julia extension (1.2.5) . It works for me too and shows the problem:
image

I don’t get any linting at all on Mac.

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