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

Value::get_uint should return a Result, not an Option #22

Open
alice-i-cecile opened this issue Jun 30, 2022 · 4 comments
Open

Value::get_uint should return a Result, not an Option #22

alice-i-cecile opened this issue Jun 30, 2022 · 4 comments

Comments

@alice-i-cecile
Copy link

There are several different ways this method can fail.

This return a Result with an enum that implements the Error trait that captures the failure mode and returns it to the end user.

@kamadak
Copy link
Owner

kamadak commented Sep 3, 2022

I see your point. On the other hand, other parts of this library do not raise errors based of the semantics of values.

How about this:

Value::as_uint(&self) -> Option<SomeType>
SomeType::get(&self, index: usize) -> Option<u32>

Then, we can do something like this:

value.as_uint().expect("not unsigned integer")
     .get(3).expect("out of range")

@alice-i-cecile
Copy link
Author

On the other hand, other parts of this library do not raise errors based of the semantics of values.

Hmm. Would it make sense to migrate these wholesale?

The core problem that we ran into here was trying to handle the errors more robustly. Your suggestion would definitely help, but it feels a little unidiomatic to not use Result when discussing failure modes.

@kamadak
Copy link
Owner

kamadak commented Sep 3, 2022

By "other parts of this library do not raise errors based of the semantics of values", I meant that this library does not (mostly) care the semantics of the values because it heavily depends on the applications and tags. I did not mean this library returns an Option instead of a Result on an error. Thus, I do not think "migrating these wholesale" makes sense.

So, Value::get_uint is a rare case.
If get_uint returns an Option::None, the failure modes cannot be distinguished as you said. However, if it returns a Result::Err on an out-of-bound index, it is inconsistent with slice::get, which returns Option::None.

Maybe this?

Value::as_uint(&self) -> Result<SomeType>
SomeType::get(&self, index: usize) -> Option<u32>

@alice-i-cecile
Copy link
Author

Yep, I'd be happy with that! And then link back-and-forth in the docs :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants