-
-
Notifications
You must be signed in to change notification settings - Fork 124
Description
What is your idea? Provide a use case.
Using expect
is great, and especially in simple validators, making the assumptions direct and to the point really simplifies the code. Sadly, if we want to have nice error messages in our validators, we can't use expect
, we instead are forced to use explicit if
statements. This is more verbose and really feels like a workaround when the feature we want to use is right there, if only we could reach for it!
expect Some(needle) = list.find(haystack, find_needle)
or fail @"Could not find needle in haystack!"
This would allow us to give custom error messages for failure situations with expect
.
The syntax provided leaves space for some future expansion, while reusing existing keywords in a way that reads naturally. There should be no ambiguity in this form, since or
is used as an infix operator, but the parsing might be tricky. Other syntax could use a different keyword, or perhaps use the notation of :
which is used elsewhere in the trace
keyword.
Another example, where pattern matching isn't used:
expect 1 + 1 == 2 or fail @"The impossible happened: mathematicians are in shambles!"
Why is it a good idea?
Doing this removes having to choose between concise error handling and having easy to debug error messages in scripts.
What is the current alternative and why is it not good enough?
The current alternative is using if directly, which requires some nested syntax, like for example:
let needle =
when list.find(haystack, find_needle) is {
Some(x) -> x
None -> fail @"Could not find needle in haystack!"
}
Or forgoing the custom error message and using the one provided by expect
by default:
expect Some(needle) = list.find(haystack, find_needle)
The former is verbose, and loses the clarity of the pattern matching, requiring a new variable name (in this case x
) to be chosen. The latter is very concise, but does not allow the choice of error message.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status