Live data from Hacker News

Why Does “=” Mean Assignment?

hillelwayne.com

111–120 of 367 posts

Re: Why Does “=” Mean Assignment?

#111
post #4

Because programming languages are typically designed for the tiny group of existing programmers than the much larger group of future programmers. The same reason unnecessary tokens exist, and 'drop' means delete in databases. It's entirely cultural.

> 'drop' means delete in databases

DROP doesn't mean DELETE.

DROP object-type> object-name> is sort of like a macro for, loosely

DELETE FROM catalog-relvar-for-object-type> WHERE name = object-name>

Except that real RDBMSs don't usually have DDL that is really equivalent to DML against system tables, and particularly (esp historically, but still in many years DBs), DDL has a different relation to transaction processing than DML, so it's a very good thing for clarity and developer intuition to not overload DML keywords for DDL operations, despite the loose similarity between CREATE/DROP in DDL and INSERT/DELETE in DML.

Re: Why Does “=” Mean Assignment?

#112
post #108

I always liked DHH's take on these sorts of arguments (paraphrasing): who the hell cares? Once you know the purpose of the '=' how often do you make mistakes reading or writing code? Whereas Java is all about protecting developers from themselves, Ruby (for example) let's you get away without variable type declaration because at the end of the day, how often do you not know whether a particular variable is a string o…

I prefer to take the middle ground: implicit typing in C#/Scala with var. Benefits of both worlds.

Re: Why Does “=” Mean Assignment?

#113

It isn't just mathematically unsound, it's the cause of many errors, especially in C. Certainly a "million dollar mistake". A classic way to guard against accidental use of assignment in logic statements is to write it "backwards", i.e.: if (2 == x) {...} Since (2 = x) won't compile, while (x = 2) will not only compile but might appear to work for a very long time.

It isn't mathematically unsound. It is just a terminal symbol in a grammar that follows a different (arbitrary) convention than mathematical notation does.

And the errors result from typing "=" when you needed "==" has nothing to do with mathematics. It is a language ergonomics issue resulting from two operators being legal in the same context and one being similar to (a prefix of) the other.

Re: Why Does “=” Mean Assignment?

#114
post #10

Earlier quoted context omitted.

There’s also <-, most commonly seen in R but draws it’s heritage from the APL keyboard

Common in Pre-Python pseudo code and algorithms.

Are you making an argument that Python has caused us all to start writing pseudo-code using `=` for assignment?

Re: Why Does “=” Mean Assignment?

#115
It's ...odd... but I've always liked the simple elegance of (Meditech's) MAGIC languages. Strict left-to-right, even in assignment and in math. Instead of "A gets 1" (A = 1), it's "1 goes to A" (1^A).

    > 1^A
    1
    > A+A^A
    2
    > IF{A+A*A^B B}
    8

Re: Why Does “=” Mean Assignment?

#116

I've been programming professionally for 25 years using mostly C inspired languages, and I will still regularly write "if(foo = bar)" on a daily basis and not notice until there's an error. It's easily the most common syntax error I write, followed closely by using commas in my for-loop as apparently it looks like a function to my fingers: "for(x=0, x<10, x++)"

Same here. I have flycheck running a linter, so I catch it a bit quicker, but single = is my single most common typo.

Re: Why Does “=” Mean Assignment?

#117

During the public comment period for the original ANSI C standard, we had at least one request to add ":=" as an alternate assignment operator. We declined, but I personally would have supported that. The use of = and == is one of my least favorite bits of C syntax.

These days, I suppose we could add the Unicode character for :=

≔ U+2254 COLON EQUALS

and there is also

⩵ U+2A75 TWO CONSECUTIVE EQUALS SIGNS

if you find typing two = characters fatiguing...

Re: Why Does “=” Mean Assignment?

#118
post #13

Another interesting way to think about it is as "match". That is try to match the stuff on the right with the stuff on the left. Take Erlang for example: 1> X = 1. 1 2> X = 2. ** exception error: no match of right hand side value 2 Notice variables are immutable (not just values themselves). Once X becomes 1, it can only match with 1 after that. You might think this is silly or annoying, why not just allow reassignme…

Elixir has borrowed the pattern matching approach from Erlang, but allows rebinds which makes code refactoring easier.

iex(1)> x = 1 1 iex(2)> x = 2 2 iex(3)> ^x = 3 (MatchError) no match of right hand side value: 3

Notice the pin operator (^) to achieve the same effect.

Re: Why Does “=” Mean Assignment?

#119

I've been programming professionally for 25 years using mostly C inspired languages, and I will still regularly write "if(foo = bar)" on a daily basis and not notice until there's an error. It's easily the most common syntax error I write, followed closely by using commas in my for-loop as apparently it looks like a function to my fingers: "for(x=0, x<10, x++)"

Don't compilers warn about assignment inside of conditionals now?

In C++17 not only can you do assignment in the conditional, you can declare new variables which have the scope of the then and else clauses.

But yes, most compilers will catch this case.

Re: Why Does “=” Mean Assignment?

#120

I've been programming professionally for 25 years using mostly C inspired languages, and I will still regularly write "if(foo = bar)" on a daily basis and not notice until there's an error. It's easily the most common syntax error I write, followed closely by using commas in my for-loop as apparently it looks like a function to my fingers: "for(x=0, x<10, x++)"

Don't compilers warn about assignment inside of conditionals now?

It's used often enough in loops like this:

    while (v = next()) { ... }
or, perhaps more usefully:

    while ((v = next()) != ERROR) { ... }
EDIT: just did a quick check, and LLVM did in fact warn me about this:

    if (x = 4) { ... }
To silence the error, you have to double up on the parenthesis, which I guess is a reasonable enough solution:

    if ((x = 4)) { ... }
Post reply on HN