On 11/15/20 12:11 AM, Steven D'Aprano wrote:
It would be good if the PEP gave a survey of the practical experience of
other languages with pattern matching:
- are there languages which require an exact match, with no left over
Β Β keys? what issues, if any, do users have with that choice?
- which languages ignore extra keys? do users of those languages
Β Β consider this feature a bug, a wart, or a feature?
If useful I can volunteer to conduct such a survey. I expect to have
some cycles this coming weekend.
I've completed my survey of how other languages use pattern matching to
match Mapping-like and dict-like types, especially focusing on whether
they ignore (πΈ) or disallow (πΉ) extra keys by default.
In general:
1. Dynamically-typed languages that support pattern matching have mostly
experimental (β ) pattern-matching implementations that all ignore extra
keys by default (πΈ) rather than disallow them (πΉ). Some popular
dynamically-typed languages don't support pattern-matching at all (βοΈ).
- Erlang/Elixir are the only languages in this category with a
non-experimental implementation.
- Ruby and JavaScript both have experimental implementations only.
2. Statically-typed languages frequently provide pattern matching
capabilities but they can't be used for Mapping-like or dict-like types
(π€·ββοΈ). Frequently the pattern matching provided *can* however be used
on association lists (ordered lists of key-value tuples) or
dataclass-like instances.
So it certainly seems at least the popular vote points toward ignoring
extra keys by default rather than disallowing them by default, which is
consistent with the current wording of PEP 634-636.
Below is the full survey summary:
Key:
* Pattern Match Approach
πΈ = Pattern-matches mappings/dicts, ignoring extra keys by default
πΉ = Pattern-matches mappings/dicts, disallowing extra keys by
default
π€·ββοΈ = Has pattern matching, but doesn't apply to a
Mapping-like or dict-like types
βοΈ = Has no pattern matching for algebraic data types (but
might for strings)
* Maturity
β = Pattern-matching syntax is advertised as "experimental"
Dynamically-typed languages:
* Erlang
πΈ
* Elixir (derived from Erlang)
πΈ
* Ruby (2.7)
πΈβ
* JavaScript (TC39 draft)
πΈβ
* PHP
βοΈ
* Lua
βοΈ
* Python (current draft of PEP 634-636)
πΈβ
Statically-typed languages:
* OCaml
π€·ββοΈ
* Scala
π€·ββοΈ
* Swift
π€·ββοΈ
* Rust
π€·ββοΈ
* Haskell
π€·ββοΈ
* C++
π€·ββοΈ
--
David Foster | Seattle, WA, USA
Contributor to TypedDict support for mypy
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/JNQD3CJD4B5LEBT3N4BNMBQ6WDDGLGTZ/
Code of Conduct: http://python.org/psf/codeofconduct/