johnynek / bosatsu

A python-ish pure and total functional programming language
Apache License 2.0
223 stars 11 forks source link

implement binding matches #1108

Closed johnynek closed 6 months ago

johnynek commented 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.

We could allow binding names if the matches is the argument of an if or 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 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.