On Mon, Nov 23, 2020 at 08:44:21PM +0000, David Mertz wrote: > Basically, I agree matching/destructuring is a powerful idea. But I also > wonder how much genuinely better it is than a library that does not require > a language change. For example, I could create a library to allow this: > > m = Matcher(arbitrary_expression) > if m.case("StringNode(s)"): > process_string(m.val) > elif m.case("[a, 5, 6, b]"): > process_two_free_vars(*m.values) > elif m.case("PairNone(a, b)"): > a, b = m.values > process_pair(a, b) > elif m.case("DictNode"): > foo = {key, process_node(child_node) for key, child_node in > m.values.items()}
Look at all those strings. So much for syntax highlighting and compile-time syntax checking. Who needs them anyway? # Perfectly legal syntax. if m.case("StringNode[s)"): ... elif m.case("[a, 5 6, b"): ... It's a good thing we already have comprehensions, because if we didn't have them, people would argue that they aren't necessary, we can just write a `comprehension` function that takes the loop expression as a string: # [(a + int(''.join(b)))*c) for a, b, c in items] comprehension("(a + int(''.join(b)))*c)", items] Imagine how silly we would be to dedicate actual syntax to comprehensions, when we can write a library to do it. It's all this syntactic sugar (comprehensions, decorators, f-strings, async, with statements, etc) that is killing Python. *wink* > I don't disagree that the pattern mini-language looks nice as syntax. But > there's nothing about that mini-language that couldn't be put in a library > (with the caveat that patterns would need to be quoted in some way). One thing that we get with pattern matching syntax is the absense of certain *misfeatures*. # Oops, I forgot to use `elif`, now I have fall-through semantics # which is widely agreed to be a bad idea. m = Matcher([4, 5, 6, 7]) if m.case("[a, 5, 6, b]"): print("first case") if m.case("[4, a, b, 7]"): print("second case") As the library author, I hope you are prepared for many, many bug reports about why people's code behaves differently if they use `if` compared to `elif`. Another misfeature: the ability to scatter your pattern matching cases all over the place. m = Matcher(expression) do_this() do_that() if m.case(something): process("this case") do_another_thing() class Spam: # snip five pages of code if m.case(something_else): print("Did you forget we were inside a match pseudo-block?") Objection: "But coders won't do that!" No, *sensible* coders won't do that. With pattern matching syntax, even the other sort of coder can't do that. Objection: "But they could put five pages of code inside a case block too." True, but only if it is *conditional* to that specific case. You can't mix unconditional code and cases. And the extra indentation will hint that something odd is going on. Another misfeature: the ability to modify the value being matched in the middle of the pattern matching pseudo-block. m = Matcher(something) if m.case(spam): process("case 1") m = Matcher(another_thing) if m.case(eggs): process("case 2") People can write obfuscated, confusing, poor-quality code with anything, but syntax can limit their opportunities to do so. -- Steve _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/UERNB6GZB4YPAHHIB57OTUBPYLQTHATA/ Code of Conduct: http://python.org/psf/codeofconduct/