Skip to content

exercises: consider replacing some errors with optionals/asserts/assumes #229

Open
@ee7

Description

@ee7

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions