For example, for binary-search, the Zig track currently requires the user to return an error when the input is empty:
|
test "nothing is found in an empty array" { |
|
try testing.expectError(SearchError.EmptyBuffer, binarySearch(u64, 13, &[_]u64{})); |
|
} |
or the value is not found:
|
test "identifies that a value is not included in the array" { |
|
try testing.expectError(SearchError.ValueAbsent, binarySearch(i32, 7, &[_]i32{ 1, 3, 4, 6, 8, 9, 11 })); |
|
} |
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:
We previously had:
I might propose removing more custom errors.
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
binarySearchreturns?usizeI'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-conjecturegrainshammingqueen-attacktriangle(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.