-
Notifications
You must be signed in to change notification settings - Fork 17
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
[RFC] Separate hard and soft pattern matching. #96
Comments
Maybe an alternative to creating separate match variants would be to have a separate construct for matching one value against multiple patterns. This would be the real equivalent to I don't have any real suggestion for what the syntax should be yet. I definitely want to avoid the implicit magic of I can't imagine choosing the A
That feels like a lot of visual noise for what's essentially |
Something I've been thinking about recently is how pattern matching could be used to better address exception handling locally. While One good example is attempting to open a file. If the file doesn't exist, or if there's some issue with trying to open the file, an appropriate exception will be raised. But, in almost every case, that error will end up being handled directly by the thing that calls I don't have any more ideas for a good syntax for this, but it seems like a good |
Im very much against using |
The operator I was suggesting would be the other way around ( Will definitely need to reconsider that one if we go with that route. |
As a more immediate solution, I'm inclined to say that Right now, I find pattern matching in anything but parameters relatively useless, since it's not really feasible to deal with the |
I'm going to backpedal on this one a bit: I think a magic
feels pretty clean and really is just syntax sugar for the
where |
…essions. `match` expressions essentially act as a syntax sugar for creating an anonymous function and immediately invoking it with some arguments.
Summary
Split the current pattern matching syntax of
=:
into two distinct types: hard and soft. Hard pattern matching will be what the current behavior is: raising aMatchError
when the match fails. Providing hard guarantees on data matches. Soft pattern matching would not raise an error, though the semantics of what should happen are not entirely clear.This would be a syntax distinction, using a different operator to distinguish hard from soft matching.
Motivation
While working on some Spec library improvements, I've been trying to use pattern matching for conditionals, and I keep running into the issue where I want to continue execution of the body, even after a failed match. Instead, the current matcher implementation raises an error immediately and will start panicking up the stack.
I could just use equality, but then I don't get the benefits of destructuring and complex matching.
Suggested change
I think the current matching syntax could be split into two variants. The current behavior would be considered "hard matching", where an error is raised if the match fails at any point. A new behavior would be added using a distinct operator for soft matching, where instead of an error, execution continues after returning some falsey value.
I would suggest that soft matching keep the current match operator,
=:
, and hard matching be given a visually louder operator, such as=!
.=!
in particular looks very similar to=:
, so it seems like a good fit, and the bang already has the connotation of being more "dangerous" or "careless".Since the semantics of hard matching won't be different from what they are currently, I won't cover them again here. Instead, here are some examples of what I think soft matching should do:
Remaining Questions
The return value of a failed soft match is debatable. Does it make sense to return
nil
? To support the semantics shown above with usage inwhen
, the result needs to be a falsey value, so eithernil
orfalse
. Returningfalse
directly feels wrong, as a successful match does not returntrue
directly, and I feel like boolean results should always be boolean results (justtrue
orfalse
, with no other possibilities).nil
, on the other hand, implies failure without a boolean connotation.Should variables created by a match still be created if the match fails? In the last example above,
a
,b
, andc
would all be created by a successful match. However, the match before it will fail, so it's match will always fail. Should these variables be created even in the case of a failed match? If so, what should their value be?More importantly, if the match fails after the first element,
a
will have been created, so should it be destroyed? How could that be guaranteed, especially with non-locals as match variables (e.g. ivars).The text was updated successfully, but these errors were encountered: