On Tue, Dec 31, 2019 at 05:18:59PM -0800, Andrew Barnert via Python-ideas wrote:
> Some languages use special syntax to mark either values or targets:
>
> let x, K, let z = vec
> x, @K, z = vec
>
> But the simplest solution is to nothing: you have to stick it in an
> expression that isn’t a valid target, or
> It’s a target. And I think that might actually work. If the pattern matching
> library includes this (or you write it yourself):
>
> def val(x): return x
>
> … then you just write this:
>
> x, val(K), z = vec
>
> Which doesn’t seem too bad.
That is a bug-magnet. It's such a powerful bug magnet, that the mere
suggestion caused me to commit three bugs in a script I was writing
*yesterday*, that's how powerful it is.
But seriously, and all hyperbole aside, that does seem too bad to me.
It's a syntactic construct which will sucker the user (i.e. me) into a
sense of false security, then bite them when they least expect it. Match
constructs seem to work:
# constants are fine
spam, 42, eggs = obj
# function calls and expressions are fine
spam, foo(arg), eggs = obj
spam, myconstant or fallback, eggs = obj
and then you write this:
spam, db['key'], eggs = obj
and the feces hits the rotary air circulating machine.
I'd almost rather have a rule that says you can only use literals for
the pattern matching than syntax which is so prone to laying landmines.
> And notice that any object with a custom __eq__ will be matched by
> calling that, so your library can have a decorator that turns an
> arbitrary function into a matcher
I'm sorry, I don't understand what that means. Can you give an example?
[...]
> > At that
> > point you might be better off with a dedicated "switch" or "case"
> > construct.
>
> Sure, if we’re willing to use up one or two new keywords and design a
> whole new syntax for them we can obviously make it do whatever we
> want.
If something is worth doing, it's worth doing right.
--
Steven
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/THP35J47UMH3NTGICBR2ZS3EHHHMYDHT/
Code of Conduct: http://python.org/psf/codeofconduct/