Live data from Hacker News

Ruby adds experimental support for rightward assignments

blog.saeloun.com

141–150 of 172 posts

Re: Ruby adds experimental support for rightward assignments

#141
post #7

> While the above pattern has become standardized, it feels somewhat unnatural as we read most of the spoken languages from left to right. Is this true (the 'unnatural' part)? Reading left to right, `age = 42` can be read as "age is 42" which feels perfectly natural in English. `42 => age` would be read as "assign 42 to age"? This feels more awkward to me. I'm not sure I understand why anyone would want Rightward ass…

A math tutor of mine was actually pretty angry at computer science for writing things like `a=1; a=2; //so 1=2 ?` I guess he'd rather have the "assign 42 to age"... Except he'd probably read `42 => age` as "42 implies age", which would probably cause him more sleepless nights ;)

I had the same issue. This is why I always comment my assignments.

    x = x + 1 // assign x to x+1

Re: Ruby adds experimental support for rightward assignments

#142

Earlier quoted context omitted.

That is interesting. How does it work when a variable is on each side? foo = 1 bar = 2 foo = bar

It will share with you its wisdom (the last line is printed by the interpreter): ?- FOO = 1, BAR = 2, FOO = BAR. false.

That’s neat. Is there a separate syntax for var to var assignment or are variables immutable in Prolog?

Re: Ruby adds experimental support for rightward assignments

#143
post #73

This is true to Ruby's Perl heritage. There is more than one way to do everything, even the most basic of operations. Quiz: what does the following Ruby code do? a = b => c = d

I've always liked the good old Perlism

    [$a => $b]->[$b 
for the minimum. (I think this is from Effective Perl Programming, but I couldn't find it at a glance.)

Re: Ruby adds experimental support for rightward assignments

#144

Earlier quoted context omitted.

That is interesting. How does it work when a variable is on each side? foo = 1 bar = 2 foo = bar

It will share with you its wisdom (the last line is printed by the interpreter): ?- FOO = 1, BAR = 2, FOO = BAR. false.

Erlang is interesting too here, in that it basically works the same way (unification essentially via substitution). If you can't make it work, a program exception is thrown instead of a false language. This all makes sense since prolog talks more about logic, and erlang is GP language.

Re: Ruby adds experimental support for rightward assignments

#145

Earlier quoted context omitted.

> I don't know how to read the arrow. As a description (which is an easier translation of this idiom into English than an imperative) “is assigned to”. Imperatively, English tends to prefer verb-object with implicit subject, so we ought to be using a Lisp family language (setq “foo” 42) maps better to English imperative structure than most popular languages that do imperative assignment.

Complete English sentences in present tense are infix, with the subject and object clearly stated. Implicit subjects only follow after the first sentence in a paragraph or a phrase. Case in point is the words you've read so far. "are", "follow" and "is" are verbs. This is why most programming languages are in infix notation. Lisp syntax stayed is a historical accident. [1] Arabic is a prefix languages, and people don…

> Complete English sentences in present tense are infix. . Implicit subjects only follow after the first sentence in a paragraph or a phrase.

This is true of the indicative mood, but not the imperative.

Giving commands in the indicative mood is not typical.

Re: Ruby adds experimental support for rightward assignments

#146
post #138

Earlier quoted context omitted.

If assignment is what we are concerned about, we should introduce something like `x := x + 1`.

why `:=` and not ` The `<-` in R comes from the APL keyboard, and that glyph is an assignment in APL. It wasn't just some crazy invention from Ross & Robert or even John Chambers.

Left side assignment is still used to us, from maths and most other programming languages, so it keeps things familar.

The `:=` has been in use in other langs like MySQL

Re: Ruby adds experimental support for rightward assignments

#147
post #7

> While the above pattern has become standardized, it feels somewhat unnatural as we read most of the spoken languages from left to right. Is this true (the 'unnatural' part)? Reading left to right, `age = 42` can be read as "age is 42" which feels perfectly natural in English. `42 => age` would be read as "assign 42 to age"? This feels more awkward to me. I'm not sure I understand why anyone would want Rightward ass…

I read it as "42 is stored in age". The works particularly well for a long chain of methods that should end in storing the resulting value in a variable. I know this might seem to be the most conventional way that Ruby is written; it makes a single statement take 8 lines instead of 1, it transforms a value over the course of the statement instead of sending messages to tell an object to transform itself, and it ignor…

wait i feel like that's actually kind of gorgeous. syntax highlighting as it exists today might not be good enough to really make that form stand out, though...

Re: Ruby adds experimental support for rightward assignments

#148
post #138

Earlier quoted context omitted.

If assignment is what we are concerned about, we should introduce something like `x := x + 1`.

why `:=` and not ` The `<-` in R comes from the APL keyboard, and that glyph is an assignment in APL. It wasn't just some crazy invention from Ross & Robert or even John Chambers.

Is "xThe same problem led early C (pre-K&R1) to change its compound assignment operators from "=op" to "op=". In early C, "x = -3" assigned the value -3 to x (as it does now), but "x=-3" meant "x = x - 3" (now written "x-=3").

Re: Ruby adds experimental support for rightward assignments

#149
post #95

Earlier quoted context omitted.

If your 10th clause is an unless then you're using it wrong. I agree that unless and the right hand assignment can be used incorrectly but that's not a reason to omit the feature :)

Where these conversations tend to break down is "you are using it wrong", which is technically correct but practically is a non sequitur. I can't control other people. Neither can you. Unfortunately saying that and acting like it's true are two separate things, and so we are chronically making decisions based on an illusion of control that is unhealthy to the point of self-harm. It just sets you up for disappointment…

There are no language features that can't "be used wrong". This argument proves too much!

Status Quo Bias is probably why we don't think of existing language features this way.

It's of course true that we're all likely to work with (or even be) incompetent people, but I really doubt individual language features has substantial impact on the damage they cause.

Re: Ruby adds experimental support for rightward assignments

#150
I expected this new feature to use the -> token but they went with => instead.

Is there any reason why they overloaded the hash rocket? Even the simple examples in the article look like somewhat malformed hashes to me!

  3 => box_height
  [170, 65] => height, weight
  traits => height, weight
How does the parser tell them apart?
Post reply on HN