From: "matz (Yukihiro Matsumoto)" <noreply@...>
Date: 2022-08-25T09:46:00+00:00
Subject: [ruby-core:109680] [Ruby master Feature#18408] Allow pattern match to set instance variables

Issue #18408 has been updated by matz (Yukihiro Matsumoto).

Status changed from Assigned to Rejected

We have discussed with @ktsj about the issue, and concluded it is intentional. Unsuccessful matches remain the assignment result from the internal matches, e.g.,

```
case [1,2,3]
in [a,2,3,4]
  # unmatch
else
  p a #=> 1 (from partial match)
end
```

Since instance variables (and global variables) can be accessed from outside, pattern matches can break the object status.

You might think it should be OK to allow instance variables in assignee of the single line pattern match, but the same issue can happen (before raising exceptions). In addition, once we allow instance variables, the request for array reference (a[1]) or attribute acess (obj.attr) would come and things would get more and more complex.

So at the moment, we will decide not to support anything but local variables.

Matz.


----------------------------------------
Feature #18408: Allow pattern match to set instance variables
https://bugs.ruby-lang.org/issues/18408#change-98904

* Author: Dan0042 (Daniel DeLorme)
* Status: Rejected
* Priority: Normal
* Assignee: ktsj (Kazuki Tsujimoto)
----------------------------------------
I expected this to work:

```ruby
42 => @v
```

But instead it raises "syntax error, unexpected instance variable"

Is this intentional?



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

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>