-
-
Notifications
You must be signed in to change notification settings - Fork 38
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
exercises: consider replacing some errors with optionals/asserts/assumes #229
Comments
This was referenced Mar 12, 2023
ee7
added a commit
to ee7/exercism-zig
that referenced
this issue
Mar 12, 2023
We simplified the error set to contain only one error [1], so there's an argument to use an optional instead. [1] 4a4665a exercises(triangle): simplify error set (2023-03-12) Refs: exercism#229
This was referenced Mar 12, 2023
ee7
added a commit
to ee7/exercism-zig
that referenced
this issue
Mar 13, 2023
Before this commit, `binary-search` required the user to return an error when the array of input items: - was empty - or did not contain the target Return an optional instead, like `std.sort.binarySearch` [1]. Now, an empty input array is just another case of "value not found". Also rename `buffer` to `items` for similar consistency with upstream [1], and to reserve the name `buffer` for a variable that is mutated by a function. [1] https://github.com/ziglang/zig/blob/adc6dec26b8b/lib/std/sort.zig#L10 Refs: exercism#229
ee7
added a commit
that referenced
this issue
Mar 15, 2023
Changes: - Make `buffer` no longer an optional. This means we now never return `SearchError.NullBuffer`, but there was no test case for that. - Make the implementation more standard, and avoid overflow when calculating `mid`. I intend to later make this exercise return an optional, not an error union. Refs: #229
ee7
added a commit
that referenced
this issue
Mar 15, 2023
The tests do not have a test case for a null `buffer`. This commit is similar to the previous commit, but is user-facing. Refs: #229
ee7
added a commit
to ee7/exercism-zig
that referenced
this issue
Mar 15, 2023
Before this commit, `binary-search` required the user to return an error when the array of input items: - was empty - or did not contain the target Return an optional instead, like `std.sort.binarySearch` [1]. Now, an empty input array is just another case of "value not found". Also rename `buffer` to `items` for similar consistency with upstream [1], and to reserve the name `buffer` for a variable that is mutated by a function. [1] https://github.com/ziglang/zig/blob/adc6dec26b8b/lib/std/sort.zig#L10 Refs: exercism#229
ee7
added a commit
that referenced
this issue
Mar 15, 2023
Before this commit, `binary-search` required the user to return an error when the array of input items: - was empty - or did not contain the target Return an optional instead, like `std.sort.binarySearch` does [1]. Now, an empty input array is just another case of "value not found". Also rename `buffer` to `items` for similar consistency with upstream [1]. Elsewhere we have used the name `buffer` only for a variable that is mutated by a function. [1] https://github.com/ziglang/zig/blob/4414f9c46e77/lib/std/sort.zig#L7-L13 Refs: #229
This was referenced Mar 23, 2023
ee7
changed the title
exercises: consider using errors less, and optional more
exercises: consider replacing some errors with optionals/asserts/assumes
Apr 8, 2023
This was referenced Sep 21, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For example, for
binary-search
, the Zig track currently requires the user to return an error when the input is empty:zig/exercises/practice/binary-search/test_binary_search.zig
Lines 56 to 58 in c5ece12
or the value is not found:
zig/exercises/practice/binary-search/test_binary_search.zig
Lines 44 to 46 in c5ece12
But we can consider the empty array to be a normal case of "value not found", in which case the only possibilities are "value found, or value not found". Then we can return an optional. The Zig stdlib does that - the implementation of
binarySearch
returns?usize
I'll propose changing this one. But we should look at all the exercises and think about whether it's better to return an optional, rather than an error union. Especially exercises where the error set is of length 1.
As of 2023-03-16, the exercises that can return a custom error are:
collatz-conjecture
grains
hamming
queen-attack
triangle
(considered in exercises(triangle): return an optional, not an error union #257)We previously had:
binary-search
(removed in exercises(binary-search): return an optional, not an error union #259)rna-transcription
(removed in exercises(rna-transcription): remove untested RnaError #258)I might propose removing more custom errors.
The text was updated successfully, but these errors were encountered: