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

Add guard expressions to pattern matching #98

Open
wants to merge 11 commits into
base: trunk
Choose a base branch
from

Conversation

tekknolagi
Copy link
Owner

@tekknolagi tekknolagi commented Jan 20, 2024

Adds guards to pattern matching:

id 2
. id =
  | x guard x==1 -> "one"
  | x -> "idk"

Where guards run in a merged version of the match case env and the env created by the pattern matching.

@tekknolagi tekknolagi temporarily deployed to scrapscript-pr-98 January 20, 2024 20:31 — with GitHub Actions Inactive
scrapscript.py Outdated Show resolved Hide resolved
scrapscript.py Outdated Show resolved Hide resolved
@tekknolagi tekknolagi temporarily deployed to scrapscript-pr-98 January 21, 2024 17:18 — with GitHub Actions Inactive
@tekknolagi tekknolagi changed the title Parse guards (ew) Add guard expressions to pattern matching Jan 21, 2024
@tekknolagi tekknolagi temporarily deployed to scrapscript-pr-98 January 21, 2024 17:22 — with GitHub Actions Inactive
@tekknolagi tekknolagi force-pushed the mb-pattern-match-guard branch from 9f73dfb to 2fc3e66 Compare January 21, 2024 17:24
@tekknolagi tekknolagi temporarily deployed to scrapscript-pr-98 January 21, 2024 17:24 — with GitHub Actions Inactive
scrapscript.py Outdated Show resolved Hide resolved
@tekknolagi
Copy link
Owner Author

TODO: add some EvalTests

@surprisetalk
Copy link

Would the question mark operator work here?

id 2
. id =
  | x ? x==1 -> "one"
  | x -> "idk"

@tekknolagi
Copy link
Owner Author

Yes, although I think it is already being used for what I originally understood as an assert. We can repurpose as guard (which won't raise/crash). Or we can rename to if.

I'm a liiiiittle wary of having a bunch of symbols

@surprisetalk
Copy link

Yeah, ? is an assert, that's why I think it fits kind of naturally there.

Regarding symbols, one of my design goals of scrapscript is to actually have no alphabetic keywords in the whole language! I don't know why this was a goal of mine, but I think it's a constraint that's encouraged "small" thinking.

@tekknolagi
Copy link
Owner Author

I think if we drop its meaning as assert and instead use it to mean guard (and fall-through on guard failure, instead of crash), sure, why not

@tekknolagi
Copy link
Owner Author

Then it's possible to write assert = | # true -> () which will raise/fail due to lack of matches

@surprisetalk
Copy link

Love it! Great idea

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.

2 participants