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/

Reply via email to