Live data from Hacker News

Why Does “=” Mean Assignment?

hillelwayne.com

311–320 of 367 posts

Re: Why Does “=” Mean Assignment?

#311
post #128
post #40

Earlier quoted context omitted.

Fun fact, R also has -> (assign to the RHS instead of LHS), and "super-assignment" >, and also can use = sometimes too (and I think has different semantics). Yay R.

I write R daily and have always used = for convenience. I can count on 0 hands how many times that has affected me. <<- has its uses though!

I find -> to be useful when poking around in the REPL but IMO it should never be used in real code.

I spent some time as a package author, that’s where I found <<- to be the most useful

Re: Why Does “=” Mean Assignment?

#312
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…

>"In other languages we might say we have assignment and destructuring ..." I'm not familiar with this term "destructuring." Can you elaborate on the concept? Might you have an example of a destructure operation and a language where its's used?

https://clojure.org/guides/destructuring

Basically it means that you can assign several variables at once, by asssigning a complex value to an aproprietly structured literal with variables as placeholders.

In pseudojavascript, let's say you got this from some API:

    var someList = [1, 2, 3, 4];
    var someMap = { name: "John Smith",
                    age: 20,
                    cars: [{ id: "AAA-1111", model: "Ford T"},
                           { id: "AAA-1112", model: "Mercedes Benz"}] }
And you want to extract useful data for your code quickly. In languages with destructuring you can do sth like this:

    var [x, y & rest] = someList; //x==1, y==2, rest == [3, 4]
    var { "name": name,
          "age": age,
          "cars": [ {"id": firstCarId, "model": firstCarModel}
                  & restOfTheCars ]
        } = someMap;
And the language will put the correct values in your variables name, age, firstCarId, firstCarModl, restOfTheCars.

Re: Why Does “=” Mean Assignment?

#313
post #308
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…

> Once you know the purpose of the '=' how often do you make mistakes reading or writing code? A nontrivial proportion of people who start trying to learn to program never get past that point, so it's worth taking them into account. > at the end of the day, how often do you not know whether a particular variable is a string or an integer? Strawman. Types are not about the difference between a string and an integer, t…

>"Strawman. Types are not about the difference between a string and an integer, they're about the difference between a user id and an order id, or a non-empty list and a possibly-empty list, or..."

Can you explain - how is the difference between a string and an integer different from the difference between a user id and an order id? You're calling the OPs comment a straw man but I'm not understanding how your examples are not the same thing given type system that understood all four of those.

Re: Why Does “=” Mean Assignment?

#314
While this is an interesting article on history, this isn't an actual criticism, is it? I've never heard this criticism.

I really don't think saying "a = 1; a = a + 1;" is the equivalent of saying "1 = 2" because "a" is a variable.

Re: Why Does “=” Mean Assignment?

#315
post #154

Earlier quoted context omitted.

In the original Smalltalk implementation, the character corresponding to _ in ASCII was a left facing arrow, which Smalltalk used for assignment at the time.

I'm not following. The corresponding ASCII code for _ is 96 no? http://www.theasciicode.com.ar/extended-ascii-code/underline...

"In the original Parc Place image, the glyph of the underscore character (_) appeared as a left-facing arrow (like in the 1963 version of the ASCII code). Smalltalk originally accepted this left-arrow as the only assignment operator. Some modern code still contains what appear to be underscores acting as assignments, hearkening back to this original usage. Most modern Smalltalk implementations accept either the underscore or the colon-equals syntax."

https://en.wikipedia.org/wiki/Smalltalk#Assignment

Re: Why Does “=” Mean Assignment?

#316

While this is an interesting article on history, this isn't an actual criticism, is it? I've never heard this criticism. I really don't think saying "a = 1; a = a + 1;" is the equivalent of saying "1 = 2" because "a" is a variable.

It's a very common criticism.

And as a teacher of programming to adult students, this causes a small amount of wasted time with the overwhelming majority of students, because they know what "=" means, even the non-mathematical ones. Usually not a huge obstacle, but a perceptible one.

And once they learn that it's a mutating assignment, some (small) fraction of students continue to forget, for an infuriatingly long time, which direction the mutation goes.

Re: Why Does “=” Mean Assignment?

#317
post #258
post #233

Earlier quoted context omitted.

The Rebol family of languages works like that.

Except that it doesn't. ":" in "x:" is not an assignment operator and "x" is not a variable.

Yes yes, it's a set-word! type in the "do" dialect. That's not how it's learned when first starting, though, and not usually the thing you're thinking about when using it moment-to-moment.

Re: Why Does “=” Mean Assignment?

#318

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++)"

Using = for equality checking in SQL doesn't help with the occasional mistakes...

Re: Why Does “=” Mean Assignment?

#319

Earlier quoted context omitted.

Depends on the dialect. It was mandatory on the Sinclair BASICs, for example (ZX81/ZX Spectrum), but optional on the Amstrad (Locomotive/Mallard) and BBC BASICs. A quick search suggests it was optional as early as "MicroSoft"'s Altair BASIC: http://swtpc.com/mholley/Altair/BasicLanguage.pdf As someone who learned first on a ZX81, I'd always assumed that "LET X=5" was the canonical form, and that "X=5" for assignment…

Likewise - but of course in Sinclair BASIC it was mandatory because every command had to begin with a keyword - a fact enforced by the fact that the cursor started out as a flashing [K], and every key on the keyboard was mapped to directly enter a keyword in that mode. LET was on the 'L' key. You literally couldn't type "a = 1" into a Spectrum/ZX81 - you couldn't type an 'a' unless you were at an [L] cursor prompt, w…

> I wonder if you, like me, were also completely thrown when you first encountered a programming language where you didn't also have to enter line numbers...

Haha, yes, totally! For me this was AMOS on the Amiga (a Basic variant). I was so thrown I started by using them anyway as they were supported by the interpreter - it just treated them as labels.

I soon stopped though when I discovered it didn't reorder based on number, so you ended up with

  10 print "world"
  5 print "hello"
  20 goto 5

Re: Why Does “=” Mean Assignment?

#320
post #267

Earlier quoted context omitted.

What are Strukturindizes?

The translation would be structure indexes.

Yeah, I think that would be clear even if I didn't speak German, which I do. I'm curious what their semantics are / what they're for.
Post reply on HN