exercism / exercism/zig

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

Open
#229 0 comments 0 reactions 1 assignee Claimed by @ee7 View on GitHub
Dominant language
Zig
Stars
73
Forks
43
Avg merge
6h 33m
Merged PRs (30d)
12

Description

For example, for `binary-search`, the Zig track currently requires the user to return an error when the input is empty:

https://github.com/exercism/zig/blob/c5ece12152f8086e77db85f825d9d673f0cda1af/exercises/practice/binary-search/test_binary_search.zig#L56-L58

or the value is not found:

https://github.com/exercism/zig/blob/c5ece12152f8086e77db85f825d9d673f0cda1af/exercises/practice/binary-search/test_binary_search.zig#L44-L46

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`][1]

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.

[1]: https://github.com/ziglang/zig/blob/36d47dd1991f0ccd7a9673075624f09500cc415e/lib/std/sort.zig#L7-L29.

---

As of 2023-03-16, the exercises that can return a custom error are:

- `collatz-conjecture`
- `grains`
- `hamming`
- `queen-attack`
- `triangle` (considered in #257)

We previously had:

- `binary-search` (removed in #259)
- `rna-transcription` (removed in #258)

I might propose removing more custom errors.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.