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

Open
#229 0 comments 0 reactions 1 assignee View on GitHub

@ee7 is already working on this.

Since Mar 12, 2023.

Assessment

This issue has not been assessed yet.

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

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 #257)

We previously had:

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

I might propose removing more custom errors.

Dominant language
Zig
Stars
73
Forks
43
Avg merge
6h 33m
Merged PRs (30d)
12

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from exercism/zig

All issues in exercism/zig

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.