[#71931] [Ruby trunk - Feature #11786] [Open] [PATCH] micro-optimize case dispatch even harder — normalperson@...

Issue #11786 has been reported by Eric Wong.

9 messages 2015/12/08

[ruby-core:72122] [Ruby trunk - Bug #11816] Partial safe navigation operator

From: mame@...
Date: 2015-12-14 23:47:11 UTC
List: ruby-core #72122
Issue #11816 has been updated by Yusuke Endoh.


It seems very sensitive to determine how long it propagates.

How should we interpret `str&.upcase + "foo"`?

If it is considered as `str&.upcase&.+("foo")`, it may return nil (as I expect).
If it is considered as `(str&.upcase) + "foo"`, it may raise an exception.  We need write `p str&.upcase&.+("foo")`.

How's that for `str&.upcase == nil`?

If it is considered as `str&.upcase&.==(nil)`, it will return false or nil.  Very confusing.
If it is considered as `(str&.upcase) == nil`, it will return true or false.

-- 
Yusuke Endoh <mame@ruby-lang.org>

----------------------------------------
Bug #11816: Partial safe navigation operator
https://bugs.ruby-lang.org/issues/11816#change-55537

* Author: Marc-Andre Lafortune
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
* ruby -v: preview 2
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
I'm extremely surprised (and disappointed) that, currently:

    x = nil
    x&.foo.bar # => NoMethodError: undefined method `bar' for nil:NilClass

To make it safe, you have to write `x&.foo&.bar`. But if `foo` is never supposed to return `nil`, then that code isn't "fail early" in case it actually does. `nil&.foo.bar` is more expressive, simpler and is perfect if you want to an error if `foo` returned `nil`. To actually get what you want, you have to resort using the old form `x && x.foo.bar`...

In CoffeeScript, you can write `x()?.foo.bar` and it will work well, since it gets compiled to

    if ((_ref = x()) != null) {
      _ref.foo.bar;
    }

All the discussion in #11537 focuses on `x&.foo&.bar`, so I have to ask:

Matz, what is your understanding of `x&.foo.bar`?

I feel the current implementation is not useful and should be changed to what I had in mind. I can't see any legitimate use of `x&.foo.bar` currently.




-- 
https://bugs.ruby-lang.org/

In This Thread

Prev Next