ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:81629] [Ruby trunk Feature#13645] Syntactic sugar for indexing when using the safe navigation operator
       [not found] <redmine.issue-13645.20170609073704@ruby-lang.org>
@ 2017-06-09  7:37 ` ndnenkov
  2017-06-09  9:52 ` [ruby-core:81634] " shevegen
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 4+ messages in thread
From: ndnenkov @ 2017-06-09  7:37 UTC (permalink / raw)
  To: ruby-core

Issue #13645 has been reported by ndn (Nikola Nenkov).

----------------------------------------
Feature #13645: Syntactic sugar for indexing when using the safe navigation operator
https://bugs.ruby-lang.org/issues/13645

* Author: ndn (Nikola Nenkov)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
# Proposal

While it works and makes sense, this is a bit cumbersome:

```ruby
hash&.[](:key)
```

Ideally, we could use something like:

```ruby
hash&.[:key]
```



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

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

* [ruby-core:81634] [Ruby trunk Feature#13645] Syntactic sugar for indexing when using the safe navigation operator
       [not found] <redmine.issue-13645.20170609073704@ruby-lang.org>
  2017-06-09  7:37 ` [ruby-core:81629] [Ruby trunk Feature#13645] Syntactic sugar for indexing when using the safe navigation operator ndnenkov
@ 2017-06-09  9:52 ` shevegen
  2017-06-09 10:13 ` [ruby-core:81636] " sawadatsuyoshi
  2019-04-16 15:01 ` [ruby-core:92305] " manga.osyo
  3 siblings, 0 replies; 4+ messages in thread
From: shevegen @ 2017-06-09  9:52 UTC (permalink / raw)
  To: ruby-core

Issue #13645 has been updated by shevegen (Robert A. Heiler).


Is this valid syntax? I ask specifically because of the '.' character there. I am not
a big fan of the & anyway though, so I am biased. I am just wondering in context of
syntax such as: 

    hash[:key]
    hash&[:key]
    hash&.[:key]

Actually I only consider the first elegant, the rest ugly. But I see your point
in regards to hash&.[](:key) versus hash&.[:key] - if the former already works
as-is, then it may make sense to allow for the latter. What I thought was that
the '.' is explicit for the method call; I guess the last example:

    hash&.[:key]

Would then be equivalent to:

    hash&.[(:key)]

right?

----------------------------------------
Feature #13645: Syntactic sugar for indexing when using the safe navigation operator
https://bugs.ruby-lang.org/issues/13645#change-65330

* Author: ndn (Nikola Nenkov)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
# Proposal

While it works and makes sense, this is a bit cumbersome:

```ruby
hash&.[](:key)
```

Ideally, we could use something like:

```ruby
hash&.[:key]
```



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

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

* [ruby-core:81636] [Ruby trunk Feature#13645] Syntactic sugar for indexing when using the safe navigation operator
       [not found] <redmine.issue-13645.20170609073704@ruby-lang.org>
  2017-06-09  7:37 ` [ruby-core:81629] [Ruby trunk Feature#13645] Syntactic sugar for indexing when using the safe navigation operator ndnenkov
  2017-06-09  9:52 ` [ruby-core:81634] " shevegen
@ 2017-06-09 10:13 ` sawadatsuyoshi
  2019-04-16 15:01 ` [ruby-core:92305] " manga.osyo
  3 siblings, 0 replies; 4+ messages in thread
From: sawadatsuyoshi @ 2017-06-09 10:13 UTC (permalink / raw)
  To: ruby-core

Issue #13645 has been updated by sawa (Tsuyoshi Sawada).


Duplicate of https://bugs.ruby-lang.org/issues/11813

----------------------------------------
Feature #13645: Syntactic sugar for indexing when using the safe navigation operator
https://bugs.ruby-lang.org/issues/13645#change-65332

* Author: ndn (Nikola Nenkov)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
# Proposal

While it works and makes sense, this is a bit cumbersome:

```ruby
hash&.[](:key)
```

Ideally, we could use something like:

```ruby
hash&.[:key]
```



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

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

* [ruby-core:92305] [Ruby trunk Feature#13645] Syntactic sugar for indexing when using the safe navigation operator
       [not found] <redmine.issue-13645.20170609073704@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2017-06-09 10:13 ` [ruby-core:81636] " sawadatsuyoshi
@ 2019-04-16 15:01 ` manga.osyo
  3 siblings, 0 replies; 4+ messages in thread
From: manga.osyo @ 2019-04-16 15:01 UTC (permalink / raw)
  To: ruby-core

Issue #13645 has been updated by osyo (manga osyo).


hi. I would like to use `hash&.[key]` (or `hash&[key]` ) in following cases as below.

```ruby
class X
  def initialize
    @hash = { a: 1, b: 2, c: 3 }
  end

  def [](key)
    @hash[key]
  end
end

def get(key)
  x = Symbol === key && X.new || nil
  # I want to use x&.[key] (or x&[key])
  x&.[](key)
end

p get(:a)     #=> 1
p get("b")    # => nil
```


Matz writes

> Use #dig for referencing the value.
> For updating, show us use cases.
> https://bugs.ruby-lang.org/issues/11813#note-6

However, YOU can not use `x&.dig(key)` for a class that Mr. Matz has previously proposed for which #dig is not defined.



----------------------------------------
Feature #13645: Syntactic sugar for indexing when using the safe navigation operator
https://bugs.ruby-lang.org/issues/13645#change-77648

* Author: ndn (Nikola Nenkov)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
# Proposal

While it works and makes sense, this is a bit cumbersome:

```ruby
hash&.[](:key)
```

Ideally, we could use something like:

```ruby
hash&.[:key]
```



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

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

end of thread, other threads:[~2019-04-16 15:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-13645.20170609073704@ruby-lang.org>
2017-06-09  7:37 ` [ruby-core:81629] [Ruby trunk Feature#13645] Syntactic sugar for indexing when using the safe navigation operator ndnenkov
2017-06-09  9:52 ` [ruby-core:81634] " shevegen
2017-06-09 10:13 ` [ruby-core:81636] " sawadatsuyoshi
2019-04-16 15:01 ` [ruby-core:92305] " manga.osyo

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).