Skip to content

Match multiple expressions in case statements#1696

Merged
paf31 merged 1 commit intopurescript:masterfrom
natefaubion:multi-case-expr
Dec 14, 2015
Merged

Match multiple expressions in case statements#1696
paf31 merged 1 commit intopurescript:masterfrom
natefaubion:multi-case-expr

Conversation

@natefaubion
Copy link
Copy Markdown
Contributor

This addresses some of the use cases of #1690 and #1687 by allowing you to match multiple expressions at once in a case statement. This is kind of a no-brainer since its already supported by the AST. Thoughts on adding this?

It's currently missing a check for binder/expression length in the branches like we do for arguments in declarations. Where should that be added?

@garyb
Copy link
Copy Markdown
Member

garyb commented Dec 8, 2015

Seems reasonable to me 👍

We could put the binder length check in Language.PureScript.Sugar.CaseDeclarations I guess, although that name is a little misleading as it's actually for desugaring top level binders into case declarations currently.

@natefaubion
Copy link
Copy Markdown
Contributor Author

Example error:

  case 42, 10, 12 of
    42, 10 -> doIt
    _ , _  -> return false
Error found:
in module CaseTest
at ...CaseTest.purs line 12, column 3 - line 16, column 1

  Binder list lengths differ in case alternatives:

    42, 10
    _, _

  Expecting 3 binders.

@natefaubion
Copy link
Copy Markdown
Contributor Author

I could also do an error for each alternative that is incorrect instead of one for the whole case. Maybe that would be more useful, so you don't have to hunt for them.

@garyb
Copy link
Copy Markdown
Member

garyb commented Dec 11, 2015

That'd be nice - I'm not exactly sure how you'd determine it though... I guess there's a few rules that might work: pick the most common arity first, if there is no common arity then default to the first pattern?

@natefaubion
Copy link
Copy Markdown
Contributor Author

I'd think whatever is in the case head would be treated as the canonical binder length.

Comment thread src/Language/PureScript/Errors.hs Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prettyPrintBinderAtom?

@paf31
Copy link
Copy Markdown
Contributor

paf31 commented Dec 11, 2015

Looks nice, thanks! Sorry for the slow response.

As for syntax, I think a comma separated list is probably best. The only other thing I can think of is to parenthesize, but that would almost certainly be more confusing for Haskellers since it's valid tuple syntax.

@paf31
Copy link
Copy Markdown
Contributor

paf31 commented Dec 11, 2015

🚢 Thanks!

@natefaubion
Copy link
Copy Markdown
Contributor Author

I've got to fix the test.

@natefaubion
Copy link
Copy Markdown
Contributor Author

Ok, should be good to go. I changed it so it emits an error for each alternative with the incorrect number of binders

Error 1 of 2:

  in module CaseTest
  at CaseTest.purs line 13, column 5 - line 13, column 12

    Binder list length differs in case alternative:

      42, 10

    Expecting 3 binders.



  See https://github.com/purescript/purescript/wiki/Error-Code-CaseBinderLengthDiffers for more information,
  or to contribute content related to this error.

Error 2 of 2:

  in module CaseTest
  at CaseTest.purs line 14, column 5 - line 14, column 12

    Binder list length differs in case alternative:

      _, _

    Expecting 3 binders.



  See https://github.com/purescript/purescript/wiki/Error-Code-CaseBinderLengthDiffers for more information,
  or to contribute content related to this error.

@paf31
Copy link
Copy Markdown
Contributor

paf31 commented Dec 13, 2015

@garyb Can we get this one merged in for 0.7.7? I'm leaving merges up to you at the moment since I know you had some ordering in mind. If so, @natefaubion, could you please merge master?

@garyb
Copy link
Copy Markdown
Member

garyb commented Dec 13, 2015

Is there going to be a 0.7.7? I figured we were pretty much there with 0.8 now anyway.

My order-specific PRs are in now, so merging whatever is fine again, although it looks like something I did means this will need rebasing first.

@paf31
Copy link
Copy Markdown
Contributor

paf31 commented Dec 13, 2015

I wanted to discuss that actually. If there is nothing actually breaking in 0.8, maybe it's fine to skip 0.7.7.

@garyb
Copy link
Copy Markdown
Member

garyb commented Dec 13, 2015

Yeah, so far everything is backwards compatible - or enough so that SlamData still compiles anyway, probably a pretty good test 😄

@natefaubion
Copy link
Copy Markdown
Contributor Author

Rebased.

@paf31
Copy link
Copy Markdown
Contributor

paf31 commented Dec 13, 2015

Thanks!

@paf31
Copy link
Copy Markdown
Contributor

paf31 commented Dec 14, 2015

Somehow this needs rebasing again. I'm not sure why, but I'll be sure to merge it immediately this time. Sorry...

@natefaubion
Copy link
Copy Markdown
Contributor Author

Done. I think it was a whitespace issue.

paf31 added a commit that referenced this pull request Dec 14, 2015
Match multiple expressions in case statements
@paf31 paf31 merged commit ab1b00d into purescript:master Dec 14, 2015
@paf31
Copy link
Copy Markdown
Contributor

paf31 commented Dec 14, 2015

👍 😌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants