Especially if Result<T, ()> is optimized into a single word, it seems ideal
to me to get rid of str::from_utf_opt(&[u8]) -> Option<~str> and just have
str::from_utf(&[u8]) -> Result<~str, ()>. This makes simple programs that
don't care about error handling a little more complicated, of course, since
it forces them to handle the error case. Although, the only extra
complication is ".unwrap()" so, its not that bad. For more complex programs
that do care about error handling it makes it explicit at the call site how
the error condition should be handled which I think is a big win. Return
code checking is a C idiom, and there is nothing wrong with that style of
error handling. However, the C compiler doesn't do much of anything to
force you to check the return codes which leads to lots of code that fails
to do so. Using a Result<>, however, forces the caller to do those checks.
Result<> still supports various chaining functions just like Option<> to
make that easier. However, you don't have to worry about accidentally
passing a None to a function that takes an Option<> as an input parameter
with non-error semantics.

Also, I like "unwrap()" since to me, nothing about "get()" says: this is an
error handling function that might lead to killing the current task. get()
sounds simple and safe, buts its not if called on an Option<>.

-Palmer Cox


On Fri, Dec 6, 2013 at 11:14 PM, Patrick Walton <pcwal...@mozilla.com>wrote:

> On 12/6/13 6:46 PM, Niko Matsakis wrote:
>
>> I agree. I've personally been moving towards `Result<T, ()>` in
>> preference to `Option<T>` when one of the branches reflects an
>> error. It's worth noting that the compiler could still optimize this
>> into a pointer-null-pointer representation, though I doubt it does so
>> now.
>>
>
> IIRC it does.
>
> Patrick
>
>
> _______________________________________________
> Rust-dev mailing list
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to