-
Notifications
You must be signed in to change notification settings - Fork 543
What E to use in stubs for Result<T, E>? #444
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
Comments
I think the goals are:
I think that means this decision process:
|
In the future, we might consider using the #![feature(never_type)]
fn f() -> Result<usize, !> {
Ok(5)
}
fn main() {
println!("Hello, world! {:?}", f());
}
#[cfg(test)]
mod test {
// Test doesn't run on playground, that's OK, I just need it to compile.
#[test]
fn t() {
assert_eq!(f(), Ok(5));
}
} |
I think my order of preference for this would be a domain-specific error type, then
My preference by far is the domain-specific types, though. I acknowledge that this involves moving some of the implementation burden from the student to the maintainers. I submit as response to that criticism that the point of an exercise is very rarely to introduce the Result monad and force the student to design their return types from scratch, and we can special-case the exercises for which that is in fact the point. |
My first priority here will be to change https://github.com/exercism/rust/tree/master/exercises/variable-length-quantity then, as its stub uses a We can continue to develop ideas about what to use for Here is another facet to think about: In the case where multiple functions may return different kinds of errors, shall we encourage using the same E or a different E for each? We'll use
Each decision does have a disadvantage. If using a single domain-specific error type and have all four functions use it as If using one error type for each of those four functions, then maybe users of the API feel it is too many error types to deal with. If it makes sense for the track's stubs to favour one approach over the other, which one and why? Or, we may remain silent on the matter, depending on how soon |
React is something of a special case, because it does have so many error types; that's unusual for exercism. That does make it useful as a test of principles. My inclination in this case is to ask what a well-engineered piece of software would look like if I were developing this for a client. The answer in this case is to have a custom error type for Given that we only really need two distinct error types, are those too many to deal with? I don't think so, but there's reasonable scope to disagree here. In that case, it may be time to introduce error-coalescing libraries such as the |
If a custom error type is to be used, it would make sense to |
std::error::Error takes some amount of busywork to implement.
Should the track maintainers implement it? If so, why? The students aren't
expected to catch these errors or send descriptions to end users; they're
expected simply to raise the appropriate errors in the appropriate
conditions.
Should the students implement it? If so, why? The test framework that
Exercism provides doesn't care about the legibility of the error messages.
I could see the benefit to someone introducing a new exercise whose purpose
was to introduce the Error trait for its coalescing properties, because
that is something useful to know about. (It could also be a fun design
challenge to write an exercise which might be solved either by using the
std Error trait or by using the one from the Failure crate; the latter may
be of interest to students because trait implementations can be derived.)
However, I don't believe that we should expect anyone to implement this
trait for their error messages as a matter of course throughout this track.
…On Sat, Mar 10, 2018 at 4:09 AM, Shing Tak Lam ***@***.***> wrote:
If a custom error type is to be used, it would make sense to impl
std::error::Error for it, which allows for better formatting of error
messages.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#444 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AHdeTrcb9v7vKU_37rHyok88KUCSHY78ks5tc0P2gaJpZM4SbBcW>
.
|
The stubs with Result are:
Of these: variable-length-quantity has already been changed to a domain-specific error, forth has always had a domain-specific error, I sent out PRs for react and ocr-numbers. all-your-base coming soon. After that, we'll look at stubs as they get contributed, and perhaps think of any other exercises whose tests may want to look for specific errors, rather than the generic |
Please feel free to refer back to this issue when deciding what should happen in any stubs that get added in the future. There are still some tests containing |
In #269 we decided that exercises should have stubs. In #371 we further decided that the provided stubs should compile.
For all exercises that have function(s) expecting a
Result<T, E>
, this poses a question: What type should go in the stub forE
?()
, but have a comment explaining that the type can be changed. Currently used in https://github.com/exercism/rust/blob/master/exercises/react/src/lib.rs (rather too late, I would think).()
, with no additional explanations.PUT_YOUR_ERROR_TYPE_HERE
. Not yet used in this track, because it would cause the stub not to compile. We are able to mark stubs as allowed to not compile (https://github.com/exercism/rust/blob/master/exercises/decimal/.meta/ALLOWED_TO_NOT_COMPILE) but it should be used sparingly.BowlingError
but leaveBowlingError
as simplypub struct BowlingError {}
.&'static str
with the idea that it's to be used as an error message. Currently used in https://github.com/exercism/rust/blob/master/exercises/variable-length-quantity/src/lib.rsI'm starting to shy away from using
str
as an error type since it is difficult to programmatically inspect, but now need to think about which of the other options I prefer. Perhaps my reason is bogus and usingstr
is okay (let me know!). A contributor once also said this as a tip, but did not provide reasoning, so I cannot evaluate the tip. #259 (comment)Need to think about the goals: Are we trying to teach something via these stubs? Or is it precisely the minimum code needed to get it to compile?
The text was updated successfully, but these errors were encountered: