From: "Eregon (Benoit Daloze)" Date: 2022-08-26T19:01:53+00:00 Subject: [ruby-core:109717] [Ruby master Feature#18408] Allow pattern match to set instance variables Issue #18408 has been updated by Eregon (Benoit Daloze). Dan0042 (Daniel DeLorme) wrote in #note-20: > But if you don't want ivars to change during match, this *can* be fixed, for example by using temporary local vars like I suggested in #note-11. I don't think this is a fundamental problem with this syntax, although it increases the cost/benefit ratio. No, it cannot be fixed in the presence of guards and arbitrary calls (notably almost anything in pattern matching calls `===`), which might access the ivar indirectly. It was already mentioned in https://bugs.ruby-lang.org/issues/18408#note-8 but it's probably clearer this way: ```ruby attr_reader :v def foo case expr in [@v] if @v.even? # this could be supported with additional implementation complexity in [@v] if v.even? # this cannot be supported in [@v] if bar % 3 == 0 # this cannot be supported in [@v, self] # this cannot be supported end def bar @v * 2 end ``` Also since this would mutate the object it could mean some pattern only matches due to some previous pattern mutating the object, definitely don't want that. For local variables we have a lot more control since we know exactly what can access the variable value. For ivar that's not true, anything which can refer to `self` can see the ivar value. ---------------------------------------- Feature #18408: Allow pattern match to set instance variables https://bugs.ruby-lang.org/issues/18408#change-98951 * 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: