News for hackers. Stuff that matters.
Re:Ruby adds experimental support for rightward assignments by iandev on Thursday September 10, 2020 @02:05PM (#24434927)
> 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 assignment.

Re:Ruby adds experimental support for rightward assignments by Willamin on Thursday September 10, 2020 @07:11PM (#24437552)
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 ignores the concept of encapsulation by moving the interesting actions into the foreground rather than being hidden within an object.

Below is an _incredibly_ contrived example. Take note how the `=> final_value` syntax is useful. The alternative is to place a `final_value = ` at the _top_ of the statement. I'll admit: placing the assignment at the top of the method chain helps point out to the reader of the code that an assignment is happening. However, more useful I think is quickly recognizing the flow of data.

  "hello"
    .chars
    .size
    .*(10980643.4)
    .to_i
    .to_s(36)
    .upcase
    => final_value
  
  puts(final_value)
  # => WORLD
Re:Ruby adds experimental support for rightward assignments by keeganpoppen on Thursday September 10, 2020 @10:49PM (#24438698)
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 by Freaky on Thursday September 10, 2020 @08:30PM (#24438033)
... and it's useful how? It obscures the assignment at the end of an arbitrarily long statement instead of giving the value a name right up front.

It feels a bit like a new method definition syntax that allows you to put the name after the `end` for... reasons.

Re:Ruby adds experimental support for rightward assignments by qbonnard on Thursday September 10, 2020 @02:40PM (#24435265)
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 ;)
Re:Ruby adds experimental support for rightward assignments by toxik on Thursday September 10, 2020 @02:50PM (#24435347)
He’d probably like a := 42 which is in a lot of languages actually! Also Erlang and other fp’s do consider = equivalence.
Re:Ruby adds experimental support for rightward assignments by Izkata on Thursday September 10, 2020 @04:36PM (#24436329)
I noticed a year or two ago when I'm pseudocoding I seem to default to "age <- 42" which I kinda like more...
Re:Ruby adds experimental support for rightward assignments by __ryan__ on Thursday September 10, 2020 @10:05PM (#24438478)
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 by xthestreams on Friday September 11, 2020 @06:38AM (#24441184)
I can't tell if this is a multiple joke or not
Re:Ruby adds experimental support for rightward assignments by danellis on Thursday September 10, 2020 @03:16PM (#24435577)
> math tutor ... angry

It's not the same language, so why should he expect the semantics to be the same?

Re:Ruby adds experimental support for rightward assignments by JadeNB on Thursday September 10, 2020 @09:09PM (#24438197)
> It's not the same language, so why should he expect the semantics to be the same?

It's a bit like discovering that inline C in your favourite non-C language isn't actually semantically correct C. If you're a mathematician, then you know, intimately and fluently, how math is spoken and written, so seeing something that looks exactly like "inline math", but isn't, is jarring.

(I don't get angry over it, but it is exactly counter to the extremely delicate and precise way mathematicians train ourselves to think about '='.)

Re:Ruby adds experimental support for rightward assignments by choward on Thursday September 10, 2020 @02:57PM (#24435424)
I'm a big fan of immutability and using constantly so I agree with your tutor.
Re:Ruby adds experimental support for rightward assignments by kevinmgranger on Thursday September 10, 2020 @02:32PM (#24435180)
If you visualize variables as "boxes" with values in them, "put 42 in age" makes sense.
Re:Ruby adds experimental support for rightward assignments by Someone on Thursday September 10, 2020 @03:49PM (#24435867)
But “age is 42” is a statement/Boolean, not a mutation. “Let age be 42” is more natural, but doesn’t sound well on reassignments (“let age now be 42”?)
Re:Ruby adds experimental support for rightward assignments by samatman on Thursday September 10, 2020 @04:23PM (#24436210)
I would say this as "set age to 42".
Re:Ruby adds experimental support for rightward assignments by postalrat on Thursday September 10, 2020 @08:28PM (#24438028)
"age is 42" may be a statement while "is age 42" may produce a boolean.

And I don't see why a statement can't describe a mutation.

Re:Ruby adds experimental support for rightward assignments by hcs on Thursday September 10, 2020 @04:16PM (#24436134)
I read assignment like "age gets 42".
Re:Ruby adds experimental support for rightward assignments by mcphage on Thursday September 10, 2020 @03:00PM (#24435461)
`42 => age` would probably be "42 is age", or if it was `42 => person.age` you'd read it "42 is person's age".
Re:Ruby adds experimental support for rightward assignments by coliveira on Thursday September 10, 2020 @07:34PM (#24437733)
That's why tcl uses:

set age 42

Re:Ruby adds experimental support for rightward assignments by ianmobbs on Thursday September 10, 2020 @02:20PM (#24435069)
Exactly how I feel. This is one of the reasons I'm not a huge fan of R, either
Re:Ruby adds experimental support for rightward assignments by syspec on Thursday September 10, 2020 @03:14PM (#24435557)
Agreed, when I read that line I found myself saying it out loud wondering if it was a typo.

`favorite_color = red' == "Favorite Color is Red"

Which feels more natural than: "Red is Favorite Color"

Re:Ruby adds experimental support for rightward assignments by Igelau on Thursday September 10, 2020 @09:05PM (#24438181)
"42 gets crammed into age"
Re:Ruby adds experimental support for rightward assignments by swiley on Thursday September 10, 2020 @03:40PM (#24435778)
42 is the age