Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why is if a guard instead of a combinator? #255

Closed
Tracked by #175
erights opened this issue Apr 10, 2022 · 11 comments · Fixed by #293
Closed
Tracked by #175

Why is if a guard instead of a combinator? #255

erights opened this issue Apr 10, 2022 · 11 comments · Fixed by #293

Comments

@erights
Copy link

erights commented Apr 10, 2022

Why is if a clause guard instead of a pattern combinator, like and, or, and with?

@ljharb
Copy link
Member

ljharb commented Apr 10, 2022

Because if doesn’t use a pattern, it takes an expression and tests its truthiness - so it wouldn’t make sense for it to be a combinator.

@erights
Copy link
Author

erights commented Apr 10, 2022

Ok, let's drop the term "combinator". What I'm really asking is if the equivalent of the following grammar would make sense

<pattern> ::=
  <pattern> ("and" <pattern>)+
  <pattern> ("or" <pattern>)+
  <pattern> ("with" <pattern>)+
  <pattern> ("if" "(" <expr> ")")+
  "if" "(" <expr> ")"
  <simplerPattern>;

Then you could nest the if in relevant subpatterns, just like the others.

@theScottyJam
Copy link
Contributor

Is there value in nesting the if? It seems anything you can do with a nested if can be easily extracted out and placed after the pattern-matching, without sacrificing anything.

@erights
Copy link
Author

erights commented Apr 10, 2022

It seems simpler to me to get rid of the guard concept.

A nested if can stop matching earlier, enabling patterns after that depend on the if condition succeeding. (just like a short circuiting && in the expression grammar enables us to use expressions on the right that are only non-erroneous if the expression on the left was truthy.)

Once this proposal moves forward, I expect that the <pattern> grammar will find other uses. I would like the if functionality to be part of that, rather than needing to use the clause-lhs for that.

Really, the first reason is the big one for me. You already have a compositional notion of pattern. Use that rather than introducing a new concept.

@ljharb
Copy link
Member

ljharb commented Apr 10, 2022

Thanks for clarifying. I can see arguments that suggestion that makes it more complex, and also arguments that it makes it simpler.

Where do you envision the pattern grammar to find other uses?

@theScottyJam
Copy link
Contributor

If short-circuiting is an argument, would there also be use cases for having an if before the pattern even starts?

For example, something like this (not sure how the syntax would actually work)

match(thing) {
  when(...): ...
  when(if (matcher != null) & { x: ${matcher} }): ...
}

@mpcsh
Copy link
Member

mpcsh commented Apr 11, 2022

Two thoughts:

  • I think the main benefit of the current grammar is that it mirrors existing if statements - programmers encountering this construct for the first time will be able to leverage the existing intuition.
  • I don't know of any current implementations of pattern matching in other languages that include a feature like this. So while I definitely see the benefit, if other languages have gotten along fine without it, I'm not sure I see the need.

@ljharb
Copy link
Member

ljharb commented Jul 8, 2022

This seems answered for now; @erights, are you satisfied to close this? Or, if not, are you content to consider it a stage 2 concern as opposed to a stage 2 blocker?

@ljharb ljharb mentioned this issue Jul 8, 2022
28 tasks
@erights
Copy link
Author

erights commented Jul 8, 2022

Although I do not consider this adequately answered, I agree it is not a stage 2 blocker.

@tabatkins
Copy link
Collaborator

And note that we can safely add a combinator-level if() in the future; nothing in the grammar should conflict with that.

@ByteEater-pl
Copy link

@theScottyJam, this seems easy, just add _ before if. It's really arbitrary nesting that's the added value @erights proposes.

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

Successfully merging a pull request may close this issue.

6 participants