ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:70854] [Ruby trunk - Feature #11537] [Open] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
@ 2015-09-18  9:29 ` shibata.hiroshi
  2015-09-18  9:31   ` [ruby-core:70855] " P.S.V.R
  2015-09-18 14:34 ` [ruby-core:70856] [Ruby trunk - Feature #11537] " rr.rosas
                   ` (42 subsequent siblings)
  43 siblings, 1 reply; 48+ messages in thread
From: shibata.hiroshi @ 2015-09-18  9:29 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been reported by Hiroshi SHIBATA.

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537

* Author: Hiroshi SHIBATA
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I somtimes write following code with rails application:

```rb
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```rb
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "?." syntax.
Ruby can't use "?." operator.

Can we use ".?" syntax. like this:

```rb
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?
  



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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:70855] Re: [Ruby trunk - Feature #11537] [Open] Introduce "Safe navigation operator"
  2015-09-18  9:29 ` [ruby-core:70854] [Ruby trunk - Feature #11537] [Open] Introduce "Safe navigation operator" shibata.hiroshi
@ 2015-09-18  9:31   ` P.S.V.R
  0 siblings, 0 replies; 48+ messages in thread
From: P.S.V.R @ 2015-09-18  9:31 UTC (permalink / raw)
  To: Ruby developers

[-- Attachment #1: Type: text/plain, Size: 1066 bytes --]

+1

On Fri, Sep 18, 2015 at 5:29 PM, <shibata.hiroshi@gmail.com> wrote:

> Issue #11537 has been reported by Hiroshi SHIBATA.
>
> ----------------------------------------
> Feature #11537: Introduce "Safe navigation operator"
> https://bugs.ruby-lang.org/issues/11537
>
> * Author: Hiroshi SHIBATA
> * Status: Open
> * Priority: Normal
> * Assignee: Yukihiro Matsumoto
> ----------------------------------------
> I somtimes write following code with rails application:
>
> ```rb
> u = User.find(id)
> if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
>   ...
> ```
>
> or
>
> ```rb
> # Use ActiveSupport
> if u.try!(:profile).try!(:thumbnails).try!(:large)
>  ...
> ```
> I hope to write shortly above code. Groovy has above operator named "Safe
> navigation operator" with "?." syntax.
> Ruby can't use "?." operator.
>
> Can we use ".?" syntax. like this:
>
> ```rb
> u = User.find(id)
> u.?profile.?thumbnails.?large
> ```
>
> Matz. How do you think about this?
>
>
>
>
> --
> https://bugs.ruby-lang.org/
>



-- 
https://github.com/pmq20

[-- Attachment #2: Type: text/html, Size: 2023 bytes --]

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:70856] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
  2015-09-18  9:29 ` [ruby-core:70854] [Ruby trunk - Feature #11537] [Open] Introduce "Safe navigation operator" shibata.hiroshi
@ 2015-09-18 14:34 ` rr.rosas
  2015-09-19  0:42 ` [ruby-core:70861] " nobu
                   ` (41 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: rr.rosas @ 2015-09-18 14:34 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Rodrigo Rosenfeld Rosas.


+1. Besides Groovy, CoffeeScript also allows that and I've used this feature a lot in my past days with Groovy and still use it all the time with CoffeeScript and have been missing this feature in Ruby for a long time.

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54236

* Author: Hiroshi SHIBATA
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I somtimes write following code with rails application:

```rb
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```rb
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "?." syntax.
Ruby can't use "?." operator.

Can we use ".?" syntax. like this:

```rb
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?
  



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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:70861] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
  2015-09-18  9:29 ` [ruby-core:70854] [Ruby trunk - Feature #11537] [Open] Introduce "Safe navigation operator" shibata.hiroshi
  2015-09-18 14:34 ` [ruby-core:70856] [Ruby trunk - Feature #11537] " rr.rosas
@ 2015-09-19  0:42 ` nobu
  2015-09-19  4:09 ` [ruby-core:70862] " matthew
                   ` (40 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: nobu @ 2015-09-19  0:42 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Nobuyoshi Nakada.

Description updated

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54241

* Author: Hiroshi SHIBATA
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:70862] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2015-09-19  0:42 ` [ruby-core:70861] " nobu
@ 2015-09-19  4:09 ` matthew
  2015-09-23 17:04 ` [ruby-core:70892] " rr.rosas
                   ` (39 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: matthew @ 2015-09-19  4:09 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Matthew Kerwin.


As per the discussion a few years ago in #8191, I'm +1 for `.?foo` syntax.

I still have a lingering question about the following; does this print "Hello" twice?

    a = Object.new
    def a.b
      puts "Hello"
    end
    a.?b.?c
    
    a && a.b && a.b.c    # this does
    a.try!(:b).try!(:c)  # this doesn't (?)

I think it should NOT call `a.b` twice.

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54242

* Author: Hiroshi SHIBATA
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:70892] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2015-09-19  4:09 ` [ruby-core:70862] " matthew
@ 2015-09-23 17:04 ` rr.rosas
  2015-10-07 14:30 ` [ruby-core:71012] " matz
                   ` (38 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: rr.rosas @ 2015-09-23 17:04 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Rodrigo Rosenfeld Rosas.


Matthew, it shouldn't be implemented as a && a.b && a.b.c, but something like (temp1 = a) && (temp2 = temp1.b) && temp2.c

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54264

* Author: Hiroshi SHIBATA
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71012] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2015-09-23 17:04 ` [ruby-core:70892] " rr.rosas
@ 2015-10-07 14:30 ` matz
  2015-10-07 16:51 ` [ruby-core:71015] " sawadatsuyoshi
                   ` (37 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: matz @ 2015-10-07 14:30 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Yukihiro Matsumoto.


I like the idea. My remaining concern is ".?" is too similar to "?." which is chosen by other languages.
We cannot use "?." since it conflicts with a method call with a "*?" predicate method.

Matz.


----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54387

* Author: Hiroshi SHIBATA
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71015] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2015-10-07 14:30 ` [ruby-core:71012] " matz
@ 2015-10-07 16:51 ` sawadatsuyoshi
  2015-10-21  7:45 ` [ruby-core:71129] " matz
                   ` (36 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: sawadatsuyoshi @ 2015-10-07 16:51 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Tsuyoshi Sawada.


The `&&` and `try` are different. I am considering the `&&` version.

Since we already have:

    a &&= b

which means

    a = a && b

By analogy from the above, and given that we want

    a && a.b

what about:

    a.&&b

or more shortly:

    a.&b

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54389

* Author: Hiroshi SHIBATA
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71129] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (6 preceding siblings ...)
  2015-10-07 16:51 ` [ruby-core:71015] " sawadatsuyoshi
@ 2015-10-21  7:45 ` matz
  2015-10-21  8:05 ` [ruby-core:71130] " matz
                   ` (35 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: matz @ 2015-10-21  7:45 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Yukihiro Matsumoto.


In several languages (Groovy Swift etc.), use `?.` but we cannot use it in Ruby, because `foo?` is a valid method name.
Thus `?.` is a reasonable alternative for Ruby, I think.

Accepted.

Matz.


----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54500

* Author: Hiroshi SHIBATA
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71130] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (7 preceding siblings ...)
  2015-10-21  7:45 ` [ruby-core:71129] " matz
@ 2015-10-21  8:05 ` matz
  2015-10-21 10:21 ` [ruby-core:71132] " rr.rosas
                   ` (34 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: matz @ 2015-10-21  8:05 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Yukihiro Matsumoto.


Oh, I made mistake. We will introduce `.?` (typo fixed already).

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54501

* Author: Hiroshi SHIBATA
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71132] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (8 preceding siblings ...)
  2015-10-21  8:05 ` [ruby-core:71130] " matz
@ 2015-10-21 10:21 ` rr.rosas
  2015-10-21 19:29 ` [ruby-core:71135] " ruby-core
                   ` (33 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: rr.rosas @ 2015-10-21 10:21 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Rodrigo Rosenfeld Rosas.


Great news! Thanks! Is this going to be released on next minor or on Ruby 3 only?

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54504

* Author: Hiroshi SHIBATA
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71135] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (9 preceding siblings ...)
  2015-10-21 10:21 ` [ruby-core:71132] " rr.rosas
@ 2015-10-21 19:29 ` ruby-core
  2015-10-21 23:33 ` [ruby-core:71137] " nobu
                   ` (32 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: ruby-core @ 2015-10-21 19:29 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Marc-Andre Lafortune.


Great!

Just to be clear, this will check only for `nil`, right? 

    nil.?foo # => nil
    false.?foo # => NoMethodError

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54507

* Author: Hiroshi SHIBATA
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71137] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (10 preceding siblings ...)
  2015-10-21 19:29 ` [ruby-core:71135] " ruby-core
@ 2015-10-21 23:33 ` nobu
  2015-10-22 16:29 ` [ruby-core:71157] " treznick
                   ` (31 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: nobu @ 2015-10-21 23:33 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Nobuyoshi Nakada.


Marc-Andre Lafortune wrote:
> Just to be clear, this will check only for `nil`, right? 

I think so, and my implementation too.

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54509

* Author: Hiroshi SHIBATA
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71157] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (11 preceding siblings ...)
  2015-10-21 23:33 ` [ruby-core:71137] " nobu
@ 2015-10-22 16:29 ` treznick
  2015-10-22 16:34 ` [ruby-core:71158] " ary
                   ` (30 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: treznick @ 2015-10-22 16:29 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Tom Reznick.


Hi,

I think we may have found some unexpected behavior with the `.?` operator.

If I call the following:
```
s = Struct.new(:x)
o = s.new()
o.x #=> nil
o.x.nil? #=> true
o.x.?nil? #=> nil
o.x.kind_of?(NilClass) #=> true
o.x.?kind_of?(NilClass) #=> nil
o.x.methods.include?(:nil?) #=> true
```

While it's arguably a bit peculiar to try to check that `nil` is `nil`, in a `nil`-safe way, `.?kind_of?(NilClass)` could reasonably return `true`.

Also this is clearly a bit of a contrived example, it does highlight some of the more unexpected behaviors of the `.?` operator.

Any chance of clarification?

All best!

Tom Reznick
Software Engineer
Continuity
@threznick

Nobuyoshi Nakada wrote:
> Applied in changeset r52214.
> 
> ----------
> Safe navigation operator
> 
> * compile.c (iseq_peephole_optimize): peephole optimization for
>   branchnil jumps.
> * compile.c (iseq_compile_each): generate save navigation operator
>   code.
> * insns.def (branchnil): new opcode to pop the tos and branch if
>   it is nil.
> * parse.y (NEW_QCALL, call_op, parser_yylex): parse token '.?'.
>   [Feature #11537]



----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54530

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71158] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (12 preceding siblings ...)
  2015-10-22 16:29 ` [ruby-core:71157] " treznick
@ 2015-10-22 16:34 ` ary
  2015-10-22 16:45 ` [ruby-core:71160] " merch-redmine
                   ` (29 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: ary @ 2015-10-22 16:34 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Ary Borenszweig.


I know this is already decided and the commit is out there, but since you are adding new syntax and a new feature to the language, I suggest you reconsider https://bugs.ruby-lang.org/issues/9076

With that change, instead of adding special syntax for safe nil traversing, you get generalized syntax for the implicit block argument. Instead of this:

```
obj.?bar(x, y)
```

You do:

```
obj.try &.bar(x, y)
```

The `try` method is simply:

```
class Object
  # But obviously implemented in C for performance reasons
  def try
    yield self unless self.is_a?(NilClass)
  end
end
```

As I mention in the original issue, `foo &.bar` simply gets translated *by the parser* to something like `foo { |x| x.bar }` (where `x` doesn't conflict with any other identifier in the method). So it's just a change in the parser, no need to change compile.c, insns.def, etc (although I can understand that nil checking might be optimized with a VM instruction).

My main worry is code like this:

```
obj.?empty?
```

That looks confusing to the eye. I associate "?" next to method names to "query" methods, and now when reading an expression "?" can have several meanings (in addition to the ternary operator).

We already have this syntax in [Crystal](http://www.crystal-lang.org) and it's working really well.

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54531

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71160] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (13 preceding siblings ...)
  2015-10-22 16:34 ` [ruby-core:71158] " ary
@ 2015-10-22 16:45 ` merch-redmine
  2015-10-22 21:19   ` [ruby-core:71166] " Matthew Kerwin
  2015-10-23  3:58 ` [ruby-core:71169] " treznick
                   ` (28 subsequent siblings)
  43 siblings, 1 reply; 48+ messages in thread
From: merch-redmine @ 2015-10-22 16:45 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Jeremy Evans.


Tom Reznick wrote:
> Hi,
> 
> I think we may have found some unexpected behavior with the `.?` operator.
> 
> If I call the following:
> 
>     s = Struct.new(:x)
>     o = s.new()
>     o.x #=> nil
>     o.x.nil? #=> true
>     o.x.?nil? #=> nil
>     o.x.kind_of?(NilClass) #=> true
>     o.x.?kind_of?(NilClass) #=> nil
>     o.x.methods.include?(:nil?) #=> true
> 
> 
> While it's arguably a bit peculiar to try to check that `nil` is `nil`, in a `nil`-safe way, `.?kind_of?(NilClass)` could reasonably return `true`.

I think it's completely expected that `nil.?kind_of?(NilClass)` returns `nil` and not `true`.  The whole point of `.?` is to return `nil` without calling the method if the receiver is `nil`.  I'm not sure if `.?` is a good idea syntax-wise, but if you are going to have it, it shouldn't have special cases for specific methods.

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54533

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71166] Re: [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
  2015-10-22 16:45 ` [ruby-core:71160] " merch-redmine
@ 2015-10-22 21:19   ` Matthew Kerwin
  0 siblings, 0 replies; 48+ messages in thread
From: Matthew Kerwin @ 2015-10-22 21:19 UTC (permalink / raw)
  To: Ruby developers

[-- Attachment #1: Type: text/plain, Size: 1147 bytes --]

On 23/10/2015 2:46 AM, <merch-redmine@jeremyevans.net> wrote:
>
> Issue #11537 has been updated by Jeremy Evans.
>
>
> Tom Reznick wrote:
> > Hi,
> >
> > I think we may have found some unexpected behavior with the `.?`
operator.
> >
> > If I call the following:
> >
> >     s = Struct.new(:x)
> >     o = s.new()
> >     o.x #=> nil
> >     o.x.nil? #=> true
> >     o.x.?nil? #=> nil
> >     o.x.kind_of?(NilClass) #=> true
> >     o.x.?kind_of?(NilClass) #=> nil
> >     o.x.methods.include?(:nil?) #=> true
> >
> >
> > While it's arguably a bit peculiar to try to check that `nil` is `nil`,
in a `nil`-safe way, `.?kind_of?(NilClass)` could reasonably return `true`.
>
> I think it's completely expected that `nil.?kind_of?(NilClass)` returns
`nil` and not `true`.  The whole point of `.?` is to return `nil` without
calling the method if the receiver is `nil`.  I'm not sure if `.?` is a
good idea syntax-wise, but if you are going to have it, it shouldn't have
special cases for specific methods.
>

I agree, or put another way: if you're testing for nil in two ways, .? has
higher priority. That makes it a programmer issue, not a ruby one.

[-- Attachment #2: Type: text/html, Size: 1560 bytes --]

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71169] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (14 preceding siblings ...)
  2015-10-22 16:45 ` [ruby-core:71160] " merch-redmine
@ 2015-10-23  3:58 ` treznick
  2015-10-26  5:10 ` [ruby-core:71182] " transfire
                   ` (27 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: treznick @ 2015-10-23  3:58 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Tom Reznick.


Thanks for the thoughtful replies guys! That definitely helps clarify the `.?` operator
Matthew Kerwin wrote:
> On 23/10/2015 2:46 AM, <merch-redmine@jeremyevans.net> wrote:
>  >
>  > Issue #11537 has been updated by Jeremy Evans.
>  >
>  >
>  > Tom Reznick wrote:
>  > > Hi,
>  > >
>  > > I think we may have found some unexpected behavior with the `.?`
>  operator.
>  > >
>  > > If I call the following:
>  > >
>  > >     s = Struct.new(:x)
>  > >     o = s.new()
>  > >     o.x #=> nil
>  > >     o.x.nil? #=> true
>  > >     o.x.?nil? #=> nil
>  > >     o.x.kind_of?(NilClass) #=> true
>  > >     o.x.?kind_of?(NilClass) #=> nil
>  > >     o.x.methods.include?(:nil?) #=> true
>  > >
>  > >
>  > > While it's arguably a bit peculiar to try to check that `nil` is `nil`,
>  in a `nil`-safe way, `.?kind_of?(NilClass)` could reasonably return `true`.
>  >
>  > I think it's completely expected that `nil.?kind_of?(NilClass)` returns
>  `nil` and not `true`.  The whole point of `.?` is to return `nil` without
>  calling the method if the receiver is `nil`.  I'm not sure if `.?` is a
>  good idea syntax-wise, but if you are going to have it, it shouldn't have
>  special cases for specific methods.
>  >
>  
>  I agree, or put another way: if you're testing for nil in two ways, .? has
>  higher priority. That makes it a programmer issue, not a ruby one.



----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54543

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71182] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (15 preceding siblings ...)
  2015-10-23  3:58 ` [ruby-core:71169] " treznick
@ 2015-10-26  5:10 ` transfire
  2015-10-26 11:22 ` [ruby-core:71184] " garysweaver
                   ` (26 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: transfire @ 2015-10-26  5:10 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Thomas Sawyer.


Yukihiro Matsumoto wrote:
> I like the idea. My remaining concern is ".?" is too similar to "?." which is chosen by other languages.
> We cannot use "?." since it conflicts with a method call with a "*?" predicate method.

Maybe there is a better syntax by requiring a space:
  
  u ? .profile ? .thumbnails ? .large

In this way is more an extension of the ternary operator -- the initial dot on the method signals the difference. This should also allow:

  u ? .profile ? .thumbnails ? .large : default



----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54562

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71184] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (16 preceding siblings ...)
  2015-10-26  5:10 ` [ruby-core:71182] " transfire
@ 2015-10-26 11:22 ` garysweaver
  2015-10-26 12:25 ` [ruby-core:71186] " wycats
                   ` (25 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: garysweaver @ 2015-10-26 11:22 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Gary Weaver.


Thomas Sawyer wrote:
>     u ? .profile ? .thumbnails ? .large : default

No, because then you can't do this, which is valid in current Ruby:

a = b = c = d = e = false
g = true
a ? b ? c : d : e ? f : g
=> true

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54564

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71186] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (17 preceding siblings ...)
  2015-10-26 11:22 ` [ruby-core:71184] " garysweaver
@ 2015-10-26 12:25 ` wycats
  2015-10-26 13:28 ` [ruby-core:71189] " nobu
                   ` (24 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: wycats @ 2015-10-26 12:25 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Yehuda Katz.


Yukihiro Matsumoto wrote:
> I like the idea. My remaining concern is ".?" is too similar to "?." which is chosen by other languages.
> We cannot use "?." since it conflicts with a method call with a "*?" predicate method.
> 
> Matz.

I agree with this concern. I also agree with the idea of trying to make it more like a traditional infix operator by encouraging spaces.

What about:

~~~
u && .profile && .thumbnails && .large
~~~

As shorthand for:

~~~
u && u.profile && u.profile.thumbnails && u.profile.thumbnails.large
~~~

It reads nicely to me and its meaning is clear. It also doesn't conflict with Ruby's method predicates (`?` suffix).

Matz, what do you think?

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54565

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71189] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (18 preceding siblings ...)
  2015-10-26 12:25 ` [ruby-core:71186] " wycats
@ 2015-10-26 13:28 ` nobu
  2015-10-26 13:58 ` [ruby-core:71190] " rr.rosas
                   ` (23 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: nobu @ 2015-10-26 13:28 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Nobuyoshi Nakada.


What does sole `.profile` do?
If it requires `&&` just before it, a space should not be between them.

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54568

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71190] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (19 preceding siblings ...)
  2015-10-26 13:28 ` [ruby-core:71189] " nobu
@ 2015-10-26 13:58 ` rr.rosas
  2015-10-27  9:13 ` [ruby-core:71213] " philip.claren
                   ` (22 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: rr.rosas @ 2015-10-26 13:58 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Rodrigo Rosenfeld Rosas.


Yehuda Katz wrote:
> As shorthand for:
> 
> ~~~
> u && u.profile && u.profile.thumbnails && u.profile.thumbnails.large
> ~~~

Actually, it would be a shorthand for something like:

~~~
u && (tmp1 = u.profile) && (tmp2 = tmp1.thumbnails) && tmp2.large
~~~

But I still prefer the syntax implemented in trunk as it's similar to other languages and will be more familiar for those used to the operator in Groovy and CoffeeScript, for example.

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54569

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71213] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (20 preceding siblings ...)
  2015-10-26 13:58 ` [ruby-core:71190] " rr.rosas
@ 2015-10-27  9:13 ` philip.claren
  2015-10-27 11:47 ` [ruby-core:71215] " rr.rosas
                   ` (21 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: philip.claren @ 2015-10-27  9:13 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Philip Claren.


Thomas Sawyer wrote:
> In this way is more an extension of the ternary operator -- the initial dot on the method signals the difference. This should also allow:
> 
>     u ? .profile ? .thumbnails ? .large : default

Although I agree that .? is not that intuitive to read, the extended ternary operator would have a problem: boolean false passes for the safe navigator (because it's a valid value) but not for the ternary operator.

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54593

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71215] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (21 preceding siblings ...)
  2015-10-27  9:13 ` [ruby-core:71213] " philip.claren
@ 2015-10-27 11:47 ` rr.rosas
  2015-10-27 12:01 ` [ruby-core:71216] " mame
                   ` (20 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: rr.rosas @ 2015-10-27 11:47 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Rodrigo Rosenfeld Rosas.


On the other hand, the problem with using "u?.profile" is that "u?" is a valid method, but if Ruby required an space for using the new operator, we could get it working very similarly to Groovy and CoffeeScript:

~~~
u ?.profile ?.thumbnails ?.large || default
~~~

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54595

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71216] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (22 preceding siblings ...)
  2015-10-27 11:47 ` [ruby-core:71215] " rr.rosas
@ 2015-10-27 12:01 ` mame
  2015-10-27 12:07 ` [ruby-core:71217] " rr.rosas
                   ` (19 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: mame @ 2015-10-27 12:01 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Yusuke Endoh.


Rodrigo Rosenfeld Rosas wrote:
> ~~~
> u ?.profile ?.thumbnails ?.large || default
> ~~~

Consider a method `+`.

`str.?+"foo"` causes no conflict, but `str ?.+"foo"` will be parsed as `str(?. + "foo")`.

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

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54596

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71217] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (23 preceding siblings ...)
  2015-10-27 12:01 ` [ruby-core:71216] " mame
@ 2015-10-27 12:07 ` rr.rosas
  2015-10-27 12:24 ` [ruby-core:71218] " mame
                   ` (18 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: rr.rosas @ 2015-10-27 12:07 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Rodrigo Rosenfeld Rosas.


I believe you are referring to implementation details and in this case I can't really opinionate, but with regards to syntax, since "?." is not a valid code, it should be interpreted as "?.+" and "foo" would be parsed as its argument.

But if this is too complicate to implement, I'm also fine with using ".?".

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54597

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71218] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (24 preceding siblings ...)
  2015-10-27 12:07 ` [ruby-core:71217] " rr.rosas
@ 2015-10-27 12:24 ` mame
  2015-10-27 12:31 ` [ruby-core:71219] " rr.rosas
                   ` (17 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: mame @ 2015-10-27 12:24 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Yusuke Endoh.


"?." is a valid code.  Try: `p ?. + "foo"`

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


----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54598

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71219] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (25 preceding siblings ...)
  2015-10-27 12:24 ` [ruby-core:71218] " mame
@ 2015-10-27 12:31 ` rr.rosas
  2015-10-27 12:45 ` [ruby-core:71220] " mame
                   ` (16 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: rr.rosas @ 2015-10-27 12:31 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Rodrigo Rosenfeld Rosas.


Thanks for pointing me that. Everyday learning a new hidden feature from Ruby ;)

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54599

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71220] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (26 preceding siblings ...)
  2015-10-27 12:31 ` [ruby-core:71219] " rr.rosas
@ 2015-10-27 12:45 ` mame
  2015-10-27 14:31 ` [ruby-core:71221] " matz
                   ` (15 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: mame @ 2015-10-27 12:45 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Yusuke Endoh.


Rodrigo Rosenfeld Rosas wrote:
> Everyday learning a new hidden feature from Ruby ;)

Me too.  Ruby has infinite possibilities.

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

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54600

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71221] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (27 preceding siblings ...)
  2015-10-27 12:45 ` [ruby-core:71220] " mame
@ 2015-10-27 14:31 ` matz
  2015-10-29 19:32 ` [ruby-core:71271] " alonecomp
                   ` (14 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: matz @ 2015-10-27 14:31 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Yukihiro Matsumoto.


I don't think `u && .profile && .thumbnails && .large` is acceptable for some reasons:

* it's longer
* unclear

than proposed `.?`.  Instead, `&.` or `.&` can be alternatives.  But my concern for the idea is `&&` does handle false values (false and nil), where proposed behavior only accepts nil.

Matz.

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54601

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71271] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (28 preceding siblings ...)
  2015-10-27 14:31 ` [ruby-core:71221] " matz
@ 2015-10-29 19:32 ` alonecomp
  2015-10-29 21:00   ` [ruby-core:71273] " Matthew Kerwin
  2015-11-04  0:01 ` [ruby-core:71322] " transfire
                   ` (13 subsequent siblings)
  43 siblings, 1 reply; 48+ messages in thread
From: alonecomp @ 2015-10-29 19:32 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Laurentiu Macovei.


The same discussion happens to be on TypeScript and ES6 worlds.
Using .. instead of ?. or .? because it's way more clear when you are using the ternary ? : operator on the same line.

If it's not a conflict in Ruby syntax perhaps worths looking at 

https://esdiscuss.org/topic/existential-operator-null-propagation-operator#content-65
https://github.com/Microsoft/TypeScript/issues/16#issuecomment-152275052

Posting the markdown info from there: 

This would be amazing operator!! Especially for `ES6`/`ES7`/`TypeScript` - and why not `Ruby` ?

```js
var error = a.b.c.d; //this would fail with error if a, b or c are null or undefined.
var current = a && a.b && a.b.c && a.b.c.d; // the current messy way to handle this
var currentBrackets = a && a['b'] && a['b']['c'] && a['b']['c']['d']; //the current messy way to handle this
var typeScript = a?.b?.c?.d; // The typescript way of handling the above mess with no errors
var typeScriptBrackets = a?['b']?['c']?['d']; //The typescript of handling the above mess with no errors
```
However I propose a more clear one - as not to confuse ? from the a ? b : c statements with a?.b statements:

```js
var doubleDots = a..b..c..d; //this would be ideal to understand that you assume that if any of a, b, c is null or undefined the result will be null or undefined.
var doubleDotsWithBrackets = a..['b']..['c']..['d'];
```

For the bracket notation, I recommend two dots instead of a single one as it's consistent with the others when non brackets are used. Hence only the property name is static or dynamic via brackets.

Two dots, means if its null or undefined stop processing further and assume the result of expression is null or undefined. (as d would be null or undefined).

Two dots make it more clear, more visible and more space-wise so you understand what's going on.

This is not messing with numbers too - as is not the same case e.g.

```js
1..toString(); // works returning '1'
var x = {};
x.1 = {y: 'test' }; //fails currently
x[1] = {y: 'test' }; //works currently 
var current = x[1].y; //works
var missing= x[2].y; //throws exception
var assume= x && x[2] && x[2].y; // works but very messy
```

About numbers two options: Your call which one can be adopted, but I recommend first one for compatibility with existing rules!
1. Should fail as it does now (`x.1.y` == `runtime error`)
```js
var err = x..1..y; // should fail as well, since 1 is not a good property name, nor a number to call a method, since it's after x object.
```
2. Should work since it understands that is not a number calling a property from `Number.prototype`
```js
var err = x..1..y; // should work as well, resulting 'test' in this case
var err = x..2..y; // should work as well, resulting undefined in this case
```


With dynamic names:
```js
var correct1 = x..[1]..y; //would work returning 'test'
var correct2 = x..[2]..y; //would work returning undefined;
```

What do you think folks?

P.S. `foo?.bar` and `foo?['bar']` syntax would work too.

However the using both current `?` `:` operator and `?.` might be very confusing on the same line.

e.g. using `?.` and `?['prop']`
```js
var a = { x: { y: 1 } };
var b = condition ? a?.x.?y : a?.y?.z;
var c = condition ? a?['x']?['y'] : a?['y']?['z'];
```
as opposed to double dots `..` and `..['prop']`
```js
var a = { x: { y: 1 } };
var b = condition ? a..x..y : a..y..z;
var c = condition ? a..['x']..['y'] : a..['y']..['z'];
```

##### Which one does look more clear to you?

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54644

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71273] Re: [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
  2015-10-29 19:32 ` [ruby-core:71271] " alonecomp
@ 2015-10-29 21:00   ` Matthew Kerwin
  0 siblings, 0 replies; 48+ messages in thread
From: Matthew Kerwin @ 2015-10-29 21:00 UTC (permalink / raw)
  To: Ruby developers

[-- Attachment #1: Type: text/plain, Size: 4838 bytes --]

a..b already means something in ruby

I'm happy with Matz's acceptance of .? especially in light of all the
discussions that lead up to it.
On 30/10/2015 5:33 AM, <alonecomp@gmail.com> wrote:

> Issue #11537 has been updated by Laurentiu Macovei.
>
>
> The same discussion happens to be on TypeScript and ES6 worlds.
> Using .. instead of ?. or .? because it's way more clear when you are
> using the ternary ? : operator on the same line.
>
> If it's not a conflict in Ruby syntax perhaps worths looking at
>
>
> https://esdiscuss.org/topic/existential-operator-null-propagation-operator#content-65
> https://github.com/Microsoft/TypeScript/issues/16#issuecomment-152275052
>
> Posting the markdown info from there:
>
> This would be amazing operator!! Especially for `ES6`/`ES7`/`TypeScript` -
> and why not `Ruby` ?
>
> ```js
> var error = a.b.c.d; //this would fail with error if a, b or c are null or
> undefined.
> var current = a && a.b && a.b.c && a.b.c.d; // the current messy way to
> handle this
> var currentBrackets = a && a['b'] && a['b']['c'] && a['b']['c']['d'];
> //the current messy way to handle this
> var typeScript = a?.b?.c?.d; // The typescript way of handling the above
> mess with no errors
> var typeScriptBrackets = a?['b']?['c']?['d']; //The typescript of handling
> the above mess with no errors
> ```
> However I propose a more clear one - as not to confuse ? from the a ? b :
> c statements with a?.b statements:
>
> ```js
> var doubleDots = a..b..c..d; //this would be ideal to understand that you
> assume that if any of a, b, c is null or undefined the result will be null
> or undefined.
> var doubleDotsWithBrackets = a..['b']..['c']..['d'];
> ```
>
> For the bracket notation, I recommend two dots instead of a single one as
> it's consistent with the others when non brackets are used. Hence only the
> property name is static or dynamic via brackets.
>
> Two dots, means if its null or undefined stop processing further and
> assume the result of expression is null or undefined. (as d would be null
> or undefined).
>
> Two dots make it more clear, more visible and more space-wise so you
> understand what's going on.
>
> This is not messing with numbers too - as is not the same case e.g.
>
> ```js
> 1..toString(); // works returning '1'
> var x = {};
> x.1 = {y: 'test' }; //fails currently
> x[1] = {y: 'test' }; //works currently
> var current = x[1].y; //works
> var missing= x[2].y; //throws exception
> var assume= x && x[2] && x[2].y; // works but very messy
> ```
>
> About numbers two options: Your call which one can be adopted, but I
> recommend first one for compatibility with existing rules!
> 1. Should fail as it does now (`x.1.y` == `runtime error`)
> ```js
> var err = x..1..y; // should fail as well, since 1 is not a good property
> name, nor a number to call a method, since it's after x object.
> ```
> 2. Should work since it understands that is not a number calling a
> property from `Number.prototype`
> ```js
> var err = x..1..y; // should work as well, resulting 'test' in this case
> var err = x..2..y; // should work as well, resulting undefined in this case
> ```
>
>
> With dynamic names:
> ```js
> var correct1 = x..[1]..y; //would work returning 'test'
> var correct2 = x..[2]..y; //would work returning undefined;
> ```
>
> What do you think folks?
>
> P.S. `foo?.bar` and `foo?['bar']` syntax would work too.
>
> However the using both current `?` `:` operator and `?.` might be very
> confusing on the same line.
>
> e.g. using `?.` and `?['prop']`
> ```js
> var a = { x: { y: 1 } };
> var b = condition ? a?.x.?y : a?.y?.z;
> var c = condition ? a?['x']?['y'] : a?['y']?['z'];
> ```
> as opposed to double dots `..` and `..['prop']`
> ```js
> var a = { x: { y: 1 } };
> var b = condition ? a..x..y : a..y..z;
> var c = condition ? a..['x']..['y'] : a..['y']..['z'];
> ```
>
> ##### Which one does look more clear to you?
>
> ----------------------------------------
> Feature #11537: Introduce "Safe navigation operator"
> https://bugs.ruby-lang.org/issues/11537#change-54644
>
> * Author: Hiroshi SHIBATA
> * Status: Closed
> * Priority: Normal
> * Assignee: Yukihiro Matsumoto
> ----------------------------------------
> I sometimes write following code with rails application:
>
> ```ruby
> u = User.find(id)
> if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
>   ...
> ```
>
> or
>
> ```ruby
> # Use ActiveSupport
> if u.try!(:profile).try!(:thumbnails).try!(:large)
>  ...
> ```
> I hope to write shortly above code. Groovy has above operator named "Safe
> navigation operator" with "`?.`" syntax.
> Ruby can't use "`?.`" operator.
>
> Can we use "`.?`" syntax. like this:
>
> ```ruby
> u = User.find(id)
> u.?profile.?thumbnails.?large
> ```
>
> Matz. How do you think about this?
>
>
>
>
> --
> https://bugs.ruby-lang.org/
>

[-- Attachment #2: Type: text/html, Size: 6284 bytes --]

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71322] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (29 preceding siblings ...)
  2015-10-29 19:32 ` [ruby-core:71271] " alonecomp
@ 2015-11-04  0:01 ` transfire
  2015-11-04  0:07 ` [ruby-core:71324] " transfire
                   ` (12 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: transfire @ 2015-11-04  0:01 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Thomas Sawyer.


Laurentiu Macovei wrote:
> The same discussion happens to be on TypeScript and ES6 worlds.
> Using `..` instead of `?.` or `.?` because it's way more clear when you are using the ternary `? :` operator on the same line.
> 
> If it's not a conflict in Ruby syntax perhaps worths looking at 

`..` wouldn't work. But that reminds me. Was `!` ever considered?

    u!profile!thumbnails!large


----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54697

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71324] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (30 preceding siblings ...)
  2015-11-04  0:01 ` [ruby-core:71322] " transfire
@ 2015-11-04  0:07 ` transfire
  2015-11-04  0:32 ` [ruby-core:71325] " nobu
                   ` (11 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: transfire @ 2015-11-04  0:07 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Thomas Sawyer.


Philip Claren wrote:
> Thomas Sawyer wrote:
> > In this way is more an extension of the ternary operator -- the initial dot on the method signals the difference. This should also allow:
> > 
> >     u ? .profile ? .thumbnails ? .large : default
> 
> Although I agree that .? is not that intuitive to read, the extended ternary operator would have a problem: boolean false passes for the safe navigator (because it's a valid value) but not for the ternary operator.

Is allowing false necessary/useful? On the other hand, if it is, then might a nil-only ternary operator be useful too (regardless of this issue)?




----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54699

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71325] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (31 preceding siblings ...)
  2015-11-04  0:07 ` [ruby-core:71324] " transfire
@ 2015-11-04  0:32 ` nobu
  2015-11-04  0:44 ` [ruby-core:71326] " matz
                   ` (10 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: nobu @ 2015-11-04  0:32 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Nobuyoshi Nakada.


Thomas Sawyer wrote:
> `..` wouldn't work. But that reminds me. Was `!` ever considered?
> 
>     u!profile!thumbnails!large

No, it's a unary operator.

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54700

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71326] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (32 preceding siblings ...)
  2015-11-04  0:32 ` [ruby-core:71325] " nobu
@ 2015-11-04  0:44 ` matz
  2015-11-05 23:01 ` [ruby-core:71360] " tom.enebo
                   ` (9 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: matz @ 2015-11-04  0:44 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Yukihiro Matsumoto.


Thomas, that reminds me of old UUCP addresses (grin).
But ,as Nobu pointed out, it can be parsed as `u(!profile(!thumbnails(!large)))` unfortunately.

Matz.


----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54701

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71360] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (33 preceding siblings ...)
  2015-11-04  0:44 ` [ruby-core:71326] " matz
@ 2015-11-05 23:01 ` tom.enebo
  2015-11-05 23:42   ` [ruby-core:71361] " Eric Wong
  2015-11-06  1:25 ` [ruby-core:71363] " matz
                   ` (8 subsequent siblings)
  43 siblings, 1 reply; 48+ messages in thread
From: tom.enebo @ 2015-11-05 23:01 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Thomas Enebo.


How about '\'?  We can pay homage to Windows file delimeter?

a\b\c

I just scanned lexer and I cannot think of a reason off the top of my head why not...what does \ mean?  who knows...what for .? mean?  I don't know that either.  This suggestion is twice as efficient though since it only uses one character.

-Tom

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54725

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71361] Re: [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
  2015-11-05 23:01 ` [ruby-core:71360] " tom.enebo
@ 2015-11-05 23:42   ` Eric Wong
  0 siblings, 0 replies; 48+ messages in thread
From: Eric Wong @ 2015-11-05 23:42 UTC (permalink / raw)
  To: Ruby developers

tom.enebo@gmail.com wrote:
> How about '\'?  We can pay homage to Windows file delimeter?
> 
> a\b\c
> 
> I just scanned lexer and I cannot think of a reason off the top of my head why not...what does \ mean?  who knows...what for .? mean?  I don't know that either.  This suggestion is twice as efficient though since it only uses one character.

It's already used for continuing long lines.

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71363] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (34 preceding siblings ...)
  2015-11-05 23:01 ` [ruby-core:71360] " tom.enebo
@ 2015-11-06  1:25 ` matz
  2015-11-06  1:50 ` [ruby-core:71364] " tom.enebo
                   ` (7 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: matz @ 2015-11-06  1:25 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Yukihiro Matsumoto.


I think about this for a while, and thinking of introducing `&.` instead of `.?`, because:

* `.?` is similar to `?.` in Swift and other languages, but is different anyway.
* Since `?` is a valid suffix of method names in Ruby, we already see a lot of question marks in our programs.
* `u&.profile` reminds us as short form of `u && u.profile`.

But behavior of `&.` should be kept, i.e. it should skip `nil` but recognize `false`.

Matz.

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54729

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71364] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (35 preceding siblings ...)
  2015-11-06  1:25 ` [ruby-core:71363] " matz
@ 2015-11-06  1:50 ` tom.enebo
  2015-11-06  2:21 ` [ruby-core:71365] " nobu
                   ` (6 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: tom.enebo @ 2015-11-06  1:50 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Thomas Enebo.


Eric Wong wrote:
> tom.enebo@gmail.com wrote:
>  > How about '\'?  We can pay homage to Windows file delimeter?
>  > 
>  > a\b\c
>  > 
>  > I just scanned lexer and I cannot think of a reason off the top of my head why not...what does \ mean?  who knows...what for .? mean?  I don't know that either.  This suggestion is twice as efficient though since it only uses one character.
>  
>  It's already used for continuing long lines.

well it is but that is not really an issue since it only will acknowledge '\' if right before end of line (lexer just says spaceSeen and loops back up to top of lexer for its next token).   What is really weird to me is if it isn't at the end of the line it returns as the token '\\'.  I see nowhere in the grammar where ruby acknowledges this as a valid token.  Any other literal escaping happens within lexing.  Did I just find a vestigial organ or am I missing something simple?

In any case it looks like Matz might have picked something other than .?, which was main reason I came up with \ as an idea (I feel reversing order from other languages will confuse them more than it will help -- so pick something different altogether).

-Tom

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54732

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71365] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (36 preceding siblings ...)
  2015-11-06  1:50 ` [ruby-core:71364] " tom.enebo
@ 2015-11-06  2:21 ` nobu
  2015-11-06  4:16 ` [ruby-core:71370] [Ruby trunk - Feature #11537] [Open] " shugo
                   ` (5 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: nobu @ 2015-11-06  2:21 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Nobuyoshi Nakada.


Binary operators implicitly continue the next line, without a backslash.

e.g.,

~~~ruby
foo +
1
~~~

is same as `foo + 1`.

If `\` were become a binary operator, there is an ambiguity.

~~~ruby
foo \
1
~~~

is `foo(1)` (current interpretation) or `foo\1`?

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54735

* Author: Hiroshi SHIBATA
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71370] [Ruby trunk - Feature #11537] [Open] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (37 preceding siblings ...)
  2015-11-06  2:21 ` [ruby-core:71365] " nobu
@ 2015-11-06  4:16 ` shugo
  2015-11-06 18:06 ` [ruby-core:71373] [Ruby trunk - Feature #11537] " rr.rosas
                   ` (4 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: shugo @ 2015-11-06  4:16 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Shugo Maeda.

Status changed from Closed to Open

Yukihiro Matsumoto wrote:
> I think about this for a while, and thinking of introducing `&.` instead of `.?`, because:

It sounds nice because &. is more friendly to syntax highlighting of my favorite editor,
but le me play devil's advocate here.

> * `.?` is similar to `?.` in Swift and other languages, but is different anyway.

This also applies to `->`, but `->` is accepted now, so it's not a strong reason.

> * Since `?` is a valid suffix of method names in Ruby, we already see a lot of question marks in our programs.

`&` also has other roles such as a binary operator and the prefix for block arguments.
The current syntax allows `x.&y` as a valid expression, whose behavior is the same as `x & y`,
so it may be confusing.



----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54739

* Author: Hiroshi SHIBATA
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71373] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (38 preceding siblings ...)
  2015-11-06  4:16 ` [ruby-core:71370] [Ruby trunk - Feature #11537] [Open] " shugo
@ 2015-11-06 18:06 ` rr.rosas
  2015-11-07  0:49 ` [ruby-core:71374] " nobu
                   ` (3 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: rr.rosas @ 2015-11-06 18:06 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Rodrigo Rosenfeld Rosas.


Yukihiro Matsumoto wrote:
> But behavior of `&.` should be kept, i.e. it should skip `nil` but recognize `false`.

I'm not sure I understood exactly what you meant by this.

Did you mean .? and &. would be implemented in the same way?

For example: false.?inexistent will raise. Should false&.inexisting raise or return false?

I'd like to see it always raising no matter you decide for ".?" or "&.". I don't see any reasons why one would like to call a method conditionally in the false object. If this happens it's must likely to be a bug from the method returning false.

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54743

* Author: Hiroshi SHIBATA
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71374] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (39 preceding siblings ...)
  2015-11-06 18:06 ` [ruby-core:71373] [Ruby trunk - Feature #11537] " rr.rosas
@ 2015-11-07  0:49 ` nobu
  2015-11-07  2:32 ` [ruby-core:71375] " ko1
                   ` (2 subsequent siblings)
  43 siblings, 0 replies; 48+ messages in thread
From: nobu @ 2015-11-07  0:49 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Nobuyoshi Nakada.


Rodrigo Rosenfeld Rosas wrote:
> Did you mean `.?` and `&.` would be implemented in the same way?

Yes.

> For example: `false.?inexistent` will raise. Should `false&.inexisting` raise or return `false`?

It will raise a `NoMethodError`.




----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54744

* Author: Hiroshi SHIBATA
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71375] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (40 preceding siblings ...)
  2015-11-07  0:49 ` [ruby-core:71374] " nobu
@ 2015-11-07  2:32 ` ko1
  2015-11-09 14:39 ` [ruby-core:71417] " tom.enebo
  2015-11-13 10:46 ` [ruby-core:71483] " uwe
  43 siblings, 0 replies; 48+ messages in thread
From: ko1 @ 2015-11-07  2:32 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Koichi Sasada.


I have weak objection because `foo&.bar` seems check nil *and* false.

Maybe this is because I read this expression as "foo and foo.bar", which expression checks also false.


----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54746

* Author: Hiroshi SHIBATA
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71417] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (41 preceding siblings ...)
  2015-11-07  2:32 ` [ruby-core:71375] " ko1
@ 2015-11-09 14:39 ` tom.enebo
  2015-11-13 10:46 ` [ruby-core:71483] " uwe
  43 siblings, 0 replies; 48+ messages in thread
From: tom.enebo @ 2015-11-09 14:39 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Thomas Enebo.


Nobuyoshi Nakada wrote:
> Binary operators implicitly continue the next line, without a backslash.
> 
> e.g.,
> 
> ~~~ruby
> foo +
> 1
> ~~~
> 
> is same as `foo + 1`.
> 
> If `\` were become a binary operator, there is an ambiguity.
> 
> ~~~ruby
> foo \
> 1
> ~~~
> 
> is `foo(1)` (current interpretation) or `foo\1`?


I am willing to withdraw this proposal and it is potentially ambiguous (although still able to be properly parsed) but I can count the number of times I have seen a rubyist use a line continuation with zero fingers :)  So I do not think they confusion would be very large.

-Tom

----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54786

* Author: Hiroshi SHIBATA
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [ruby-core:71483] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator"
       [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
                   ` (42 preceding siblings ...)
  2015-11-09 14:39 ` [ruby-core:71417] " tom.enebo
@ 2015-11-13 10:46 ` uwe
  43 siblings, 0 replies; 48+ messages in thread
From: uwe @ 2015-11-13 10:46 UTC (permalink / raw)
  To: ruby-core

Issue #11537 has been updated by Uwe Kubosch.


Yukihiro Matsumoto wrote:
> I think about this for a while, and thinking of introducing `&.` instead of `.?`, because:
> 
> * `.?` is similar to `?.` in Swift and other languages, but is different anyway.
> * Since `?` is a valid suffix of method names in Ruby, we already see a lot of question marks in our programs.
> * `u&.profile` reminds us as short form of `u && u.profile`.
> 
> But behavior of `&.` should be kept, i.e. it should skip `nil` but recognize `false`.

I must say I am very happy with this change.  "&." is much easier to read than ".?" .


----------------------------------------
Feature #11537: Introduce "Safe navigation operator"
https://bugs.ruby-lang.org/issues/11537#change-54847

* Author: Hiroshi SHIBATA
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I sometimes write following code with rails application:

```ruby
u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...
```

or

```ruby
# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...
```
I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax.
Ruby can't use "`?.`" operator.

Can we use "`.?`" syntax. like this:

```ruby
u = User.find(id)
u.?profile.?thumbnails.?large
```

Matz. How do you think about this?




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

^ permalink raw reply	[flat|nested] 48+ messages in thread

end of thread, other threads:[~2015-11-13 10:16 UTC | newest]

Thread overview: 48+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-11537.20150918092909@ruby-lang.org>
2015-09-18  9:29 ` [ruby-core:70854] [Ruby trunk - Feature #11537] [Open] Introduce "Safe navigation operator" shibata.hiroshi
2015-09-18  9:31   ` [ruby-core:70855] " P.S.V.R
2015-09-18 14:34 ` [ruby-core:70856] [Ruby trunk - Feature #11537] " rr.rosas
2015-09-19  0:42 ` [ruby-core:70861] " nobu
2015-09-19  4:09 ` [ruby-core:70862] " matthew
2015-09-23 17:04 ` [ruby-core:70892] " rr.rosas
2015-10-07 14:30 ` [ruby-core:71012] " matz
2015-10-07 16:51 ` [ruby-core:71015] " sawadatsuyoshi
2015-10-21  7:45 ` [ruby-core:71129] " matz
2015-10-21  8:05 ` [ruby-core:71130] " matz
2015-10-21 10:21 ` [ruby-core:71132] " rr.rosas
2015-10-21 19:29 ` [ruby-core:71135] " ruby-core
2015-10-21 23:33 ` [ruby-core:71137] " nobu
2015-10-22 16:29 ` [ruby-core:71157] " treznick
2015-10-22 16:34 ` [ruby-core:71158] " ary
2015-10-22 16:45 ` [ruby-core:71160] " merch-redmine
2015-10-22 21:19   ` [ruby-core:71166] " Matthew Kerwin
2015-10-23  3:58 ` [ruby-core:71169] " treznick
2015-10-26  5:10 ` [ruby-core:71182] " transfire
2015-10-26 11:22 ` [ruby-core:71184] " garysweaver
2015-10-26 12:25 ` [ruby-core:71186] " wycats
2015-10-26 13:28 ` [ruby-core:71189] " nobu
2015-10-26 13:58 ` [ruby-core:71190] " rr.rosas
2015-10-27  9:13 ` [ruby-core:71213] " philip.claren
2015-10-27 11:47 ` [ruby-core:71215] " rr.rosas
2015-10-27 12:01 ` [ruby-core:71216] " mame
2015-10-27 12:07 ` [ruby-core:71217] " rr.rosas
2015-10-27 12:24 ` [ruby-core:71218] " mame
2015-10-27 12:31 ` [ruby-core:71219] " rr.rosas
2015-10-27 12:45 ` [ruby-core:71220] " mame
2015-10-27 14:31 ` [ruby-core:71221] " matz
2015-10-29 19:32 ` [ruby-core:71271] " alonecomp
2015-10-29 21:00   ` [ruby-core:71273] " Matthew Kerwin
2015-11-04  0:01 ` [ruby-core:71322] " transfire
2015-11-04  0:07 ` [ruby-core:71324] " transfire
2015-11-04  0:32 ` [ruby-core:71325] " nobu
2015-11-04  0:44 ` [ruby-core:71326] " matz
2015-11-05 23:01 ` [ruby-core:71360] " tom.enebo
2015-11-05 23:42   ` [ruby-core:71361] " Eric Wong
2015-11-06  1:25 ` [ruby-core:71363] " matz
2015-11-06  1:50 ` [ruby-core:71364] " tom.enebo
2015-11-06  2:21 ` [ruby-core:71365] " nobu
2015-11-06  4:16 ` [ruby-core:71370] [Ruby trunk - Feature #11537] [Open] " shugo
2015-11-06 18:06 ` [ruby-core:71373] [Ruby trunk - Feature #11537] " rr.rosas
2015-11-07  0:49 ` [ruby-core:71374] " nobu
2015-11-07  2:32 ` [ruby-core:71375] " ko1
2015-11-09 14:39 ` [ruby-core:71417] " tom.enebo
2015-11-13 10:46 ` [ruby-core:71483] " uwe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).