scrapscript icon indicating copy to clipboard operation
scrapscript copied to clipboard

Add guard expressions to pattern matching

Open tekknolagi opened this issue 2 years ago • 7 comments

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 avatar Jan 20 '24 20:01 tekknolagi

TODO: add some EvalTests

tekknolagi avatar Jan 23 '24 14:01 tekknolagi

Would the question mark operator work here?

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

surprisetalk avatar Jan 24 '24 16:01 surprisetalk

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

tekknolagi avatar Jan 24 '24 19:01 tekknolagi

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.

surprisetalk avatar Jan 24 '24 20:01 surprisetalk

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 avatar Jan 24 '24 20:01 tekknolagi

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

tekknolagi avatar Jan 24 '24 20:01 tekknolagi

Love it! Great idea

surprisetalk avatar Jan 24 '24 21:01 surprisetalk