Project

General

Profile

Actions

Feature #17724

closed

Make the pin operator support instance/class/global variables

Added by jnchito (Junichi Ito) 7 months ago. Updated 3 months ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:102851]

Description

When I use pattern match with instance variables, I get an error message like "expecting local variable or method"

@n = 1
case 1
in ^@n
  # ...
end
#=> syntax error, unexpected instance variable, expecting local variable or method (SyntaxError)
#   in ^@n
#       ^~

However using method is not allowed, either.

def n = 1
case 1
in ^n
  # ...
end
#=> n: no such local variable (SyntaxError)

I think the message "expecting local variable or method" is confusing and should be fixed like "expecting local variable."

Updated by marcandre (Marc-Andre Lafortune) 7 months ago

Agreed. Ideally instance variable would be allowed, no?

Updated by ktsj (Kazuki Tsujimoto) 6 months ago

  • Assignee set to ktsj (Kazuki Tsujimoto)
  • Status changed from Open to Assigned

Updated by jeremyevans0 (Jeremy Evans) 5 months ago

I've added a pull request to directly support instance/class/global variables with the pin operator: https://github.com/ruby/ruby/pull/4502

You could already use all of those variables with the pin expression support by surrounding them in parantheses. However, since local variables and constants are supported, I think it makes sense to also support instance/class/global variables directly. I don't think it makes sense to support methods, as doing so is much more complex (arguments/blocks). Users can use the pin expression support for methods.

My approach to fixing this bug is really introducing a new feature so the bug reported (confusing error message) isn't a problem anymore. Since this changes the pattern matching syntax, it probably should be approved by both ktsj (Kazuki Tsujimoto) and matz (Yukihiro Matsumoto)).

Updated by ktsj (Kazuki Tsujimoto) 5 months ago

  • Backport deleted (2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN)
  • ruby -v deleted (ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin20])
  • Subject changed from Pattern matching error message is confusing to Make the pin operator support instance/class/global variables
  • Tracker changed from Bug to Feature

I think it makes sense to also support instance/class/global variables directly.

I agree.

What do you think, matz (Yukihiro Matsumoto)?

Updated by matz (Yukihiro Matsumoto) 3 months ago

Sounds reasonable. Accepted.

Matz.

Actions #6

Updated by jeremyevans (Jeremy Evans) 3 months ago

  • Status changed from Assigned to Closed

Applied in changeset git|fa87f72e1e84e2b55516be188f00434a683b924c.


Add pattern matching pin support for instance/class/global variables

Pin matching for local variables and constants is already supported,
and it is fairly simple to add support for these variable types.

Note that pin matching for method calls is still not supported
without wrapping in parentheses (pin expressions). I think that's
for the best as method calls are far more complex (arguments/blocks).

Implements [Feature #17724]

Actions

Also available in: Atom PDF