-
Notifications
You must be signed in to change notification settings - Fork 33
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
Pattern matching to-do #335
Comments
For |
Perhaps I think we also need to improve the default behavior when there are duplicate or keyword names. Some options:
I also feel like the most common case of duplicate names is when we have bound them to constants already. Something that would help in this sort of scenario is to not actually bind variables if they don't get used in the scope of the match. This would also pair nicely with Option 5 above; we'd like to build this array only if it gets used. |
#375 implemented Option 4 above, which I agree seems like a good choice. A simple workaround for |
Allow pattern matching for reserved word keys by binding to inaccessible refs. Link: #335
Allow pattern matching for reserved word keys by binding to inaccessible refs. Link: #335
What about adding the same functionality as in #444? To allow something like: switch a
m := /(\d)/
return m[1] |
any plans for guard clauses other than simple binary operators? |
@JUSTIVE Yes! I don't think we've settled on a notation yet, but I also may be forgetting a proposal. One obvious thing to allow would be switch x
& > 0 // equivalent to just "> 0"
isCool(&) // lets you invoke an arbitrary function The latter is assuming something like #480 gets implemented, so If we just want to write a function condition, we need to somehow distinguish from a regular identifier. For example, |
Great to hear that! I just needed the latter one (invoking a function), and the switch x
isCool(&)
"Something"
isCool //equivalent to above
"Nothing" |
The trouble is that a pattern of just
You're arguing for option (2). If you'd asked me, I would have guessed that we already implemented (3), as it's what's symmetric with the current meaning of |
Came back to this thread, about a year later. Been using the Civet lightly with joy so far, and I'm writing a post introducing the Civet in my language now, by comparing its features against alternatives (raw js, and typescript with ts-pattern).
Sorry for forgetting to reply, I was asking for option 2 then, but now I see why it's difficult to do. I was naively thinking 'What if the matching symbol's type is a function, why not let it apply the value?'. and of course, just assumed that 'The function would be a predicate function'. isCool(&) and match when evaluated value is truthy/falsy seems pretty legit to me now. back to my writings, while comparing with ts-pattern, the Civet's pattern matching also lacks some complicated guard clause, just like F# and ts-pattern. Maybe sounds familiar as old comments above, but I'm imagining something like this: /* just an option type */
type some<T> = { __type: "some", value: T }
type none = { __type: "none" }
type option<T> = some<T> | none
const grade =
(score: option<number>) =>
switch score
{ __type: "some", value } if value > 80
"A"
{ __type: "some", value } if value > 60
"B"
{ __type: "some", value } if value > 40
"C"
{ __type: "some", value }
"E"
{ __type: "none" }
"F" the point is, having some conditional expression next to the pattern to narrow down the example might be inappropriate since the type variant is just some and none, so here's a better one. const fruitQualifyingResult =
(value: Fruits) =>
switch value
{ __type : "Apple", weigth } if weight > 500
{ __type : "Banana", length } if length > 15
{ __type : "WaterMelon", sweetness } if sweetness > 16
{ __type : "Ok", value }
else
{ __type : "No", value }
|
(transcribing these from Discord)
[]
const
:class
makes invalidconst
:The text was updated successfully, but these errors were encountered: