Feature #11816

Updated by nobu (Nobuyoshi Nakada) over 3 years ago

I'm extremely surprised (and disappointed) that, currently:


x = nil

x& # => 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&` 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 &&`...

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


if ((_ref = x()) != null) {;


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

Matz, what is your understanding of `x&`?

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&` currently.