Hacker News new | ask | show | jobs
by rco8786 2094 days ago
This is all great stuff. I’m rather meh on RBS, mainly because separating types from code is less than ideal but I like the potential here.

But the right hand assignment operator. What on earth. Nobody asked for that and nobody wants it. Why.

3 comments

Ruby already had a sort of limited form of rightward assignment, in exception rescue:

    begin
      #... do something
    rescue StandardError => ex
      #... handle exception
    end
The ex here can be any assignment expression, it's not just a lexical variable. So you could already do this:

    class Guru
      def meditate=(exception)
        puts "caught #{exception.inspect}"
      end
    end

    # later ...
    rescue OutOfCheeseError => Guru.new.meditate
i.e. creating a nice opportunity for passing error handlers around as an argument; or, this worrying idea:

    rescue TransactionError => Thread.current[:exception]
and now I'm afraid you can do this:

    Cat = Struct.new(:name) do
      class << self
        def all() = @all ||= Set.new
        def adopt=(...)
          all << new(...)
        end
      end
    end

    "Astrid" => Cat.adopt
Oh the error handler example is kinda cool, especially paired with inline rescues

    def my_method
        .. code ..
    rescue FileNotFoundError => FileNotFoundHandler
    rescue StandardError => StandardErrorHandler
    end
But yea, as is so often the case with ruby, you can really shoot yourself if you try hard enough :P
What's strange is that it seems to have been an offshoot of the pipeline operator (|>) work from last year[1], which was originally proposed as having an assignment syntax like

  foo |> bar(20) |> (x)
where x is assigned to. People didn't like that syntax, and there were issues with = being higher precedence than |> [2]. This new right-assignment operator was created in response to that [3].

The weird thing is that the pipeline operator got killed, so the original reason for r-assign no longer exists. It looks to me like there's just some people on the Ruby team who took a shine to it.

[1]: https://bugs.ruby-lang.org/issues/15799 [2]: https://bugs.ruby-lang.org/issues/15799#note-22 [3]: https://bugs.ruby-lang.org/issues/15921

The examples given were all things that would be nice to do in the REPL. What I don’t understand is why not just define it in the repl, then, like ‘_’.
Because changing or pre-processing the language syntax is far beyond the scope of any REPL. Assigning the latest return value to a variable named '_' is very straightforward and does not require dealing with the syntax.