-
Notifications
You must be signed in to change notification settings - Fork 790
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 error tcActivePatternArgumentCountNotMatch #16846
Add error tcActivePatternArgumentCountNotMatch #16846
Conversation
❗ Release notes required
|
I think "return value" works, however "1 return value" in the error message looks a bit awkward. |
My feedback for the wording: For the return value, I would use the wording: |
What about this? For parametrized single case active pattern and partial active pattern: This active pattern requires %d parameter(s) and returns %d value, but the usage here matches %d argument(s) and %d return value. For else: This active pattern returns %d value, but the usage here matches %d return value(s). |
That looks better to me. |
It's a good idea to improve this error. At the same time, as a non-user of active patterns, I got lost even with the new message. |
Right. Although it's arguably somewhat unfortunate that total active patterns differ from partial/Boolean-returning ones in this way. These were the four messages I originally had in my comment, the third of which sounds like it would be used for partial/Boolean-returning parameterized patterns:
I.e., (3) would be used for: // Expects: 1 expression argument, 0 pattern arguments.
let (|P|_|) expr2 expr1 = Some P
match expr1 with P -> …
// ↑
// This active pattern expects 1 expression argument(s), e.g., 'P e1...'.
match expr1 with P expr2 expr3 -> …
// ↑
// This active pattern expects 1 expression argument(s), e.g., 'P e1...'. |
|
@Tangent-90
I think this is a pretty solid improvement over the status quo. I guess since we do have the expected number of expression and pattern arguments available to us, we could in theory even offer something like I wonder whether it would be worth doing something like: // Use a prebuilt expr args string for these that has an example with the actual number of args.
"This active pattern expects %d expression argument(s), e.g., '%s%s'."
"This active pattern expects %d expression argument(s) and a pattern argument, e.g., '%s%s pat'." and let fmtExprArgs paramCount =
let rec loop i (sb: StringBuilder) =
let cutoff = 10
if i > paramCount then sb.ToString()
elif i > cutoff then sb.Append("...").ToString()
else loop (i + 1) (sb.Append(" e").Append i)
loop 1 (StringBuilder())
let msg =
match paramCount, returnCount with
| 0, 0 -> FSComp.SR.tcActivePatternArgsCountNotMatchNoArgsNoPat(caseName, caseName)
| 0, _ -> FSComp.SR.tcActivePatternArgsCountNotMatchOnlyPat(caseName)
| _, 0 -> FSComp.SR.tcActivePatternArgsCountNotMatchArgs(paramCount, fmtExprArgs paramCount, caseName)
| _, _ -> FSComp.SR.tcActivePatternArgsCountNotMatchArgsAndPat(paramCount, fmtExprArgs paramCount, caseName) giving
etc. Or would that be overkill? What do you think @psfinaki? |
In the topic of diagnostics, it's hard to do an overkill, it's more about perfectionism here now :) The current state of the art is definitely an improvement compared to the main. "Use 'IsEven' instead of 'IsEven x' probably captures most of the usecases and make the error very clear. The other errors importantly contain the expected number of "things" (arguments, expressions, patterns) and also list all these terms, like hey, there are all these capabilities of active patterns, read the docs if you're interested. So @Tangent-90 if you're not tired yet, feel free to use Brian's addition. If you are tired, feel free to leave things as they are. I'd just ask to add the tests for the new errors, among other things they can serve as documentation for the time being. |
message updated and test added @brianrourkeboll @psfinaki |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good stuff, thanks for this. Active patterns are much more usable now :)
Build errors are unrelated, we hope to resolve those soon, sorry. |
Description
Add a new error to improve readability for active pattern case argument count mismatch.
This active pattern case needs %d argument(s) and %d return value, but here has %d argument(s) and %d return value.
QUESTION: what is the best name for the "return value" of an active pattern case?
before:
after:
Fixes # (issue, if applicable)
Checklist
Test cases added
Performance benchmarks added in case of performance changes
Release notes entry updated: