Closed johnynek closed 6 months ago
relates to #467
Currently, the matches syntax requires no binding names, so foo matches Foo(_, Bar(_)) works, but foo matches Foo(a, Bar(b)) would not.
matches
foo matches Foo(_, Bar(_))
foo matches Foo(a, Bar(b))
We could allow binding names if the matches is the argument of an if or elif
if
elif
so:
if foo matches Foo(a, Bar(b)): fn(a, b) else: 0
The argument against this is that it creates two ways to write the same thing because we could also write:
match foo: case Foo(a, Bar(b)): fn(a, b) case _: 0
it's bad to have two ways to write the same thing.
But, the alternative is that it can sometimes help with avoiding stair-casing:
if foo matches Foo(a, Bar(b)): f(a, b) elif baz matches Baz(c): g(c) else: 0
Here, since we are changing what we are matching in the branches, the equivalent match syntax is pretty ugly:
match
match foo: case Foo(a, Bar(b)): f(a, b) case _: match baz: case Baz(c): g(c) case _: 0
Secondly, the syntax seems like something that should work given that we have the matches syntax.
relates to #467
Currently, the
matches
syntax requires no binding names, sofoo matches Foo(_, Bar(_))
works, butfoo matches Foo(a, Bar(b))
would not.We could allow binding names if the matches is the argument of an
if
orelif
so:
The argument against this is that it creates two ways to write the same thing because we could also write:
it's bad to have two ways to write the same thing.
But, the alternative is that it can sometimes help with avoiding stair-casing:
Here, since we are changing what we are matching in the branches, the equivalent
match
syntax is pretty ugly:Secondly, the syntax seems like something that should work given that we have the
matches
syntax.