Skip to content
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(rna-transcription): remove untested RnaError #258

Merged
merged 4 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Instructions append

## Input validation

This exercise does not require you to validate the input DNA strand.
Each tested input contains only the characters `A`, `C`, `G`, or `T`.
11 changes: 2 additions & 9 deletions exercises/practice/rna-transcription/.meta/example.zig
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
const std = @import("std");
const mem = std.mem;

pub const RnaError = error{
IllegalDnaNucleotide,
};

pub fn toRna(allocator: mem.Allocator, dna: []const u8) ![]const u8 {
pub fn toRna(allocator: mem.Allocator, dna: []const u8) mem.Allocator.Error![]const u8 {
var rna_slice = try allocator.alloc(u8, dna.len);
for (dna) |dna_nucleotide, i| {
switch (dna_nucleotide) {
'A' => rna_slice[i] = 'U',
'C' => rna_slice[i] = 'G',
'G' => rna_slice[i] = 'C',
'T' => rna_slice[i] = 'A',
else => {
allocator.free(rna_slice);
return RnaError.IllegalDnaNucleotide;
},
else => unreachable,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this (unreachable) might be good to put in a HINTS.md file?

Copy link
Member Author

@ee7 ee7 Mar 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, but unreachable isn't required here. The behavior is just unspecified, in both the instructions and tests.

For example, the user can just return on an unexpected character:

else => return rna_slice,

or skip that character, leaving it undefined:

else => continue,

or set a placeholder character:

else => rna_slice[i] = '_',

For the purposes of an example in this repo, I think unreachable is best - it signals to the maintainer that the behavior is untested.

But yes, I think it's a good idea to add a hint saying that the behavior is unspecified for an unexpected character. I'll do that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done via instructions.append.md: ebab9a1

}
}
return rna_slice;
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/rna-transcription/rna_transcription.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Import the appropriate standard library and modules

pub fn toRna(allocator: mem.Allocator, dna: []const u8) (RnaError || mem.Allocator.Error)![]const u8 {
pub fn toRna(allocator: mem.Allocator, dna: []const u8) mem.Allocator.Error![]const u8 {
_ = allocator;
_ = dna;
@compileError("please implement the toRna function");
Expand Down