Project

General

Profile

Actions

Bug #11618

closed

Safe call syntax with aref or aset is

Added by yui-knk (Kaneko Yuichiro) about 9 years ago. Updated over 5 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.3.0dev (2015-10-24 trunk 52128)
[ruby-dev:49312]

Description

This test pass

# ruby -v: ruby 2.3.0dev (2015-10-24 trunk 52128) [x86_64-darwin14]
# test/ruby/test_call.rb

  def test_safe_call_aref_aset
    s = Struct.new(:x, :y)
    o = s.new({a: 10})

    assert_equal(10, o.x.?send(:[], :a))
    o.x.?send(:[]=, :a, 11)
    assert_equal(11, o.x.?send(:[], :a))

    assert_nil(o.y.?send(:[], :a))
    o.y.?send(:[]=, :a, 11)
    assert_nil(o.y.?send(:[], :a))
  end

This is invalid syntax

# ruby -v: ruby 2.3.0dev (2015-10-24 trunk 52128) [x86_64-darwin14]
# test/ruby/test_call.rb

  def test_safe_call_aref_aset
    s = Struct.new(:x, :y)
    o = s.new({a: 10})

    assert_equal(10, o.x.?[:a])
    o.x.?[:a] = 11
    assert_equal(11, o.x.?[:a])

    assert_nil(o.y.?[:a])
    o.y.?[:a] = 11
    assert_nil(o.y.?[:a])
 end
./test/runner.rb: ruby/ruby/test/ruby/test_call.rb:60: syntax error, unexpected '[', expecting '(' (SyntaxError)
    assert_equal(10, o.x.?[:a])
                           ^
ruby/ruby/test/ruby/test_call.rb:61: syntax error, unexpected '[', expecting '('
    o.x.?[:a] = 11
          ^
ruby/ruby/test/ruby/test_call.rb:62: syntax error, unexpected '[', expecting '('
    assert_equal(11, o.x.?[:a])
                           ^
ruby/ruby/test/ruby/test_call.rb:64: syntax error, unexpected '[', expecting '('
    assert_nil(o.y.?[:a])
                     ^
ruby/ruby/test/ruby/test_call.rb:65: syntax error, unexpected '[', expecting '('
    o.y.?[:a] = 11
          ^
ruby/ruby/test/ruby/test_call.rb:66: syntax error, unexpected '[', expecting '('
    assert_nil(o.y.?[:a])
                     ^
make: *** [yes-test-all] Error 1

Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #11813: Extend safe navigation operator for [] and []= with syntax sugarRejectedmatz (Yukihiro Matsumoto)Actions

Updated by matz (Yukihiro Matsumoto) about 9 years ago

I am not going to add safe navigation for aref and aset.
Maybe you can use a.?[](x) (method call of an operator).

Matz.

Actions #2

Updated by yui-knk (Kaneko Yuichiro) about 9 years ago

  • Related to Feature #11813: Extend safe navigation operator for [] and []= with syntax sugar added
Actions #3

Updated by jeremyevans0 (Jeremy Evans) over 5 years ago

  • Status changed from Open to Rejected
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0