Issue #17411 has been updated by marcandre (Marc-Andre Lafortune).


mame (Yusuke Endoh) wrote in #note-8:
> ktsj (Kazuki Tsujimoto) wrote in #note-7:
> > If there were a obvious reason, I'd like to know that.
> 
> I can think of three reasons.
> 
> * (1) This feature makes an exhaustive check impossible (like a guard), which is not related to Ruby's pattern matching.
> * (2) It is difficult for a compiler to generate efficient code. For example, it cannot create a jump table statically. But we already have a pinning operator, so this may not be related to Ruby.
> * (3) The semantics will become very complicated when the expressions have side-effects.

We also already have all theses issues/possibilities (including modifying local variables on the fly):

```ruby
var = 42
proc = ->(obj) { var = 666; true }
case [:foo, 42]
in [^proc, ^var]
  puts "match"
else
  puts "no match"
end
# => no match (currently)
```

I don't see an issue with this: don't shoot yourself in the foot...

> For (3), I think that the evaluation order should be unspecified; expression patterns may or may not be evaluated at any time (including compilation time). Otherwise, it would be very difficult or even almost impossible to optimize pattern matching. But I'm unsure if "the evaluation order is unspecifed" is acceptable for Ruby.

By "compilation time" you mean "parse time"? That does not seem acceptable; it has to be at runtime.

As for order, we can leave it unspecified, or decide that order should be by priority (say basic patterns first, then pin variable & expressions, and guards last) and left to right within a priority group? It seems that it is what all implementations should prefer doing.


----------------------------------------
Feature #17411: Allow expressions in pattern matching
https://bugs.ruby-lang.org/issues/17411#change-89619

* Author: zverok (Victor Shepelev)
* Status: Open
* Priority: Normal
* Assignee: ktsj (Kazuki Tsujimoto)
----------------------------------------
Code:

```ruby
version = {name: '2.6', released_at: Time.new(2018, 12, 25)}
version in {released_at: Time.new(2010)..Time.new(2020)}
#                            ^ syntax error, unexpected '.', expecting '}'

# This works:
range = Time.new(2010)..Time.new(2020)
version in {released_at: ^range}
#=> true
```
(Fails with all versions of the pattern matching, `in`, `=>` and `case ... in`, and on Ruby 2.7 too.)

Am I missing something about the syntax?..



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