-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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 std.error_set
helpers (containsAll
,Excluding
,Intersect
)
#19092
Conversation
14b47f9
to
ce74e68
Compare
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.
To match std.enums
maybe rename it to std.error_sets
?
ce74e68
to
7f7d907
Compare
Resolved the conflicts but left the name alone for now. From the OP:
Unsure which is better. |
It seems that #19414 (most likely, haven't confirmed that's the specific change that matters) caused the code in this PR to start erroring with things like:
I've added a commit that fixes those errors (c05ad17) but I'm not sure it's fully correct, as it seems to be using a reference to a |
@squeek502 From my understanding the recommended solution would be to copy the array value to a local
Now I'm also not sure why the newly added commit, which moves the values from one |
triggers the |
@squeek502 hi! Yes, sorry, you're hitting a latent bug exposed by my recent changes. I'm actively working on rewriting comptime pointer access which should fix the issue. In the meantime, if you have a workaround that works, stick with it and throw a TODO comment there -- I'll open a tracking issue later today. |
c05ad17
to
19ebd59
Compare
Existing std methods call it |
@Pyrolistical Thank you for that data point, IMO it makes sense to unify naming. |
+1 for Edit: Nevermind, I forgot that "complement" is also used in math, as in "the complement of A in B" which is equal to |
Instead of redefining the error set from scratch just to exclude a few errors, we can define the error set in terms of existing explicit error sets.
19ebd59
to
e2fc9c1
Compare
Rebased + removed the commit that was working around #19452 |
+1 on the difference/intersection naming! In addition to precedence in standard library, "difference" feels more precise; I immediately know what a set difference is, it denotes properties of the operation (non-commutativity), and it is also more in-line with operation names such as e.g. union. |
@squeek502 Thanks for doing the update! It's been a while since writing this, and it now looks a bit unnecessary to me to have |
Same; I have no strong feelings one way or the other about |
I don't think it's needed enough to add to the standard library. |
Do you mean the motivating use-case in e2fc9c1 isn't compelling, or that such a use-case should be handled inline instead of introducing a helper namespace? |
I think the use case isn't compelling because listing the error set explicitly is easy to maintain and encourages auditing and trimming down of error codes. When an error set becomes too big, it's better to introduce categories and diagnostics structs rather than amassing an enormous error set and then trying to use set theory to manage it. |
I'm not sure how that would help in this situation. The problem with Unless I'm thinking about it wrong, moving to a diagnostic pattern would just shift the problem from an error set to an enum or a more nebulous type, so I'm not sure it's a solution. In other words, as long as |
As per recommendation by @squeek502 in #19008 (comment) ,
this PR adds utiliy helper functions in a new module
std.error_set
to the standard library, which closes #19008 .The naming of
std.error_set
IMO makesstd.error_set.contains(Set, err)
sound more natural thanerror_sets.contains
/contained
,though precedent in
std.enums
would have lead tostd.error_sets
.I've written it the way I privately write code, with explicit asserts for invalid inputs and same-line comments,
so it's clear which incorrect usage scenarios were considered.
From what I remember other places in
std
have kept assertions at a minimum,so feel free to remove / tell me to remove any you deem unnecessary.
Also note that the current implementations of
Excluding
andIntersect
are based on comptime-executedinline for
.With accepted proposal #2473 implemented, we could instead more directly communicate them to the compiler:
Additionally, the status-quo type system doesn't have a way to express a restricted non-exhaustive error set,
so
Excluding(anyerror, error{A})
can't express thaterror.A
is excluded and has to returnanyerror
again.This alternative implementation would however not be coupled with
std.builtin.Type
structure and be naturally compatible with such upgrades.