[#98621] Re: Function getlogin_r()'s protoype] — Bertram Scharpf <lists@...>
FYI,
3 messages
2020/06/02
[#98947] [Ruby master Feature#16986] Anonymous Struct literal — ko1@...
Issue #16986 has been reported by ko1 (Koichi Sasada).
66 messages
2020/06/26
[#98962] [Ruby master Bug#16988] Kernel.load loads file from current directory without '.' in path — misharinn@...
Issue #16988 has been reported by TheSmartnik (Nikita Misharin).
5 messages
2020/06/26
[#98969] [Ruby master Feature#16994] Sets: shorthand for frozen sets of symbols / strings — marcandre-ruby-core@...
Issue #16994 has been reported by marcandre (Marc-Andre Lafortune).
7 messages
2020/06/26
[#100117] [Ruby master Feature#16994] Sets: shorthand for frozen sets of symbols / strings
— matz@...
2020/09/25
Issue #16994 has been updated by matz (Yukihiro Matsumoto).
[ruby-core:98779] [Ruby master Feature#14912] Introduce pattern matching syntax
From:
paradox.ver5@...
Date:
2020-06-13 00:57:48 UTC
List:
ruby-core #98779
Issue #14912 has been updated by ParadoxV5 (𝕻𝔞𝔯𝔞𝔡𝔬𝔵Ⅴ⓹ ⛥).
Sorry for necroposting.
* Instead of `in Constant(pattern…)` and `in Constant[pattern]` – I agree that both of which might coïncide with capitalized methods, consider `when Constant in pattern…`.
* Similarly, for an empty Hash pattern, instead of implementing `in {}` as a special case (I might be in the wrong ticket), `when {}` already does most if not all of the job.
---
* Comment: This holds true for pretty much every deconstructable class:
```ruby
module Enumerable # Not in 2.7.1 somehow
alias :deconstruct :to_a
end
```
* Question: Why enforcing `NoMatchingPatternError`? _Enforcing_ duck-typing?
```ruby
case arg
in String
warn deprecated
…
in Numeric
raise NotImplementedError, 'to be implemented'
else end # ← reserved pseudo-keyword?
```
----------------------------------------
Feature #14912: Introduce pattern matching syntax
https://bugs.ruby-lang.org/issues/14912#change-86136
* Author: ktsj (Kazuki Tsujimoto)
* Status: Assigned
* Priority: Normal
* Assignee: ktsj (Kazuki Tsujimoto)
----------------------------------------
I propose new pattern matching syntax.
# Pattern syntax
Here's a summary of pattern syntax.
```
# case version
case expr
in pat [if|unless cond]
...
in pat [if|unless cond]
...
else
...
end
pat: var # Variable pattern. It matches any value, and binds the variable name to that value.
| literal # Value pattern. The pattern matches an object such that pattern === object.
| Constant # Ditto.
| var_ # Ditto. It is equivalent to pin operator in Elixir.
| (pat, ..., *var, pat, ..., id:, id: pat, ..., **var) # Deconstructing pattern. See below for more details.
| pat(pat, ...) # Ditto. Syntactic sugar of (pat, pat, ...).
| pat, ... # Ditto. You can omit the parenthesis (top-level only).
| pat | pat | ... # Alternative pattern. The pattern matches if any of pats match.
| pat => var # As pattern. Bind the variable to the value if pat match.
# one-liner version
$(pat, ...) = expr # Deconstructing pattern.
```
The patterns are run in sequence until the first one that matches.
If no pattern matches and no else clause, NoMatchingPatternError exception is raised.
## Deconstructing pattern
This is similar to Extractor in Scala.
The patten matches if:
* An object have #deconstruct method
* Return value of #deconstruct method must be Array or Hash, and it matches sub patterns of this
```
class Array
alias deconstruct itself
end
case [1, 2, 3, d: 4, e: 5, f: 6]
in a, *b, c, d:, e: Integer | Float => i, **f
p a #=> 1
p b #=> [2]
p c #=> 3
p d #=> 4
p i #=> 5
p f #=> {f: 6}
e #=> NameError
end
```
This pattern can be used as one-liner version like destructuring assignment.
```
class Hash
alias deconstruct itself
end
$(x:, y: (_, z)) = {x: 0, y: [1, 2]}
p x #=> 0
p z #=> 2
```
# Sample code
```
class Struct
def deconstruct; [self] + values; end
end
A = Struct.new(:a, :b)
case A[0, 1]
in (A, 1, 1)
:not_match
in A(x, 1) # Syntactic sugar of above
p x #=> 0
end
```
```
require 'json'
$(x:, y: (_, z)) = JSON.parse('{"x": 0, "y": [1, 2]}', symbolize_names: true)
p x #=> 0
p z #=> 2
```
# Implementation
* https://github.com/k-tsj/ruby/tree/pm2.7-prototype
* Test code: https://github.com/k-tsj/ruby/blob/pm2.7-prototype/test_syntax.rb
# Design policy
* Keep compatibility
* Don't define new reserved words
* 0 conflict in parse.y. It passes test/test-all
* Be Ruby-ish
* Powerful Array, Hash support
* Encourage duck typing style
* etc
* Optimize syntax for major use case
* You can see several real use cases of pattern matching at following links :)
* https://github.com/k-tsj/power_assert/blob/8e9e0399a032936e3e3f3c1f06e0d038565f8044/lib/power_assert.rb#L106
* https://github.com/k-tsj/pattern-match/network/dependents
--
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>