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

Rename foo_opt to foo and drop the old foo behavior #11129

Closed
wants to merge 10 commits into from

Conversation

SimonSapin
Copy link
Contributor

On 2013-12-06, I wrote to the rust-dev mailing list:

Subject: Let’s avoid having both foo() and foo_opt()

We have some functions and methods such as std::str::from_utf8 that may succeed and give a result, or fail when the input is invalid.

  1. Sometimes we assume the input is valid and don’t want to deal with the error case. Task failure works nicely.
  2. Sometimes we do want to do something different on invalid input, so returning an Option<T> works best.

And so we end up with both from_utf8 and from_utf8. This particular case is worse because we also have from_utf8_owned and from_utf8_owned_opt, to cover everything.

Multiplying names like this is just not good design. I’d like to reduce this pattern.

Getting behavior 1. when you have 2. is easy: just call .unwrap() on the Option. I think we should rename every foo_opt() function or method to just foo, remove the old foo() behavior, and tell people (through documentation) to use foo().unwrap() if they want it back?

The downsides are that unwrap is more verbose and gives less helpful error messages on task failure. But I think it’s worth it.

The email discussion has gone around long enough. Let’s discuss a concrete proposal. For the following functions or methods, I removed foo (that caused task failure) and renamed foo_opt (that returns Option) to just foo.

Vector methods:

  • get_opt (rename only, get did not exist as it would have been just [])
  • head_opt
  • last_opt
  • pop_opt
  • shift_opt
  • remove_opt

std::path::BytesContainer method:

  • container_as_str_opt

std::str functions:

  • from_utf8_opt
  • from_utf8_owned_opt (also remove the now unused not_utf8 condition)

Is there something else that should recieve the same treatement?

I did not rename recv_opt on channels based on @brson’s feedback.

Feel free to pick only some of these commits.

@alexcrichton
Copy link
Member

I'm a little worried about this patch. This introduces a lot of call to unwrap(), which is in theory supposed to be an antipattern. It's possible that it's just too much of a pain to alter all the code that uses these functions right now, but I am afraid of promoting the unwrap() methodology in real programs because of these api changes. It seems marginally better than failure in a library because we can at least say "you were asking for it", but I'm also uncomfortable in that we don't have a solution for problems like this.

In general this seems like an improvement, but I think this warrants discussion to ensure that this it the direction that we want to go in. It would be sad for unwrap() to become rust's "null pointer problem", but that definitely seems to be where it's headed right now.

@alexcrichton
Copy link
Member

I have added this to the next meeting agenda, but this can certainly be discussed before then.

@SimonSapin
Copy link
Contributor Author

Thanks @alexcrichton . Yes, I definitely expect this needs more discussion.

@liigo
Copy link
Contributor

liigo commented Dec 24, 2013

This will make code full of .unwrap() in some situation, which is not a good smell. We should not take a "not a good smell" to replace another "not a good smell". The right direction is: find a better resolution first. -- I will share my solution later.

(Just as the "do" expr, someone want to remove it, and leave us a more ugly syntax. The right direction is, find a better syntax before remove do-expr.)

@kud1ing
Copy link

kud1ing commented Dec 24, 2013

Maybe we don't need full blown Monads/do-expressions.
Could we add a macro to achieve C# monadic null-checking http://adamralph.com/2013/12/06/ndc-diary-day-3/ ?

@liigo
Copy link
Contributor

liigo commented Dec 24, 2013

#11133 [auto-unwrap]
After it is accepted, we may safely remove one of .foo() and .foo_opt().

@alexcrichton
Copy link
Member

We decided in today's meeting that we'd like to merge this (all commits).

If you've run out of time, I'm fine to take over and rebase!

@SimonSapin
Copy link
Contributor Author

@alexcrichton I’ll do the rebase. Is there some other API that should get the same treatement?

@alexcrichton
Copy link
Member

This is certainly good for now. We can weed out more cases as we see them. We're going to adopt this as a general design principle from now on, so we'll prevent future things from coming in.

Basically, just rebasing this is fine for now, thanks so much!

@SimonSapin
Copy link
Contributor Author

I now notice that std::vec has a few more methods that fail on empty vectors/slices:

  • shift_ref
  • pop_ref
  • mut_last
  • mut_shift_ref
  • mut_pop_ref

Maybe they should return Option, to be consistent with how this PR is changing shift, pop and last.

@SimonSapin
Copy link
Contributor Author

@alexcrichton Rebased, but still building (again) to run make check

@alexcrichton
Copy link
Member

It looks like bors's queue isn't so full right now, so we can just run this through bors.

Thanks again for all this awesome work, and not everything has to change this time around, the remaining methods can be redone in follow-up PRs

bors added a commit that referenced this pull request Jan 21, 2014
[On 2013-12-06, I wrote to the rust-dev mailing list](https://mail.mozilla.org/pipermail/rust-dev/2013-December/007263.html):

> Subject: Let’s avoid having both foo() and foo_opt()
>
> We have some functions and methods such as [std::str::from_utf8](http://static.rust-lang.org/doc/master/std/str/fn.from_utf8.html) that may succeed and give a result, or fail when the input is invalid.
>
> 1. Sometimes we assume the input is valid and don’t want to deal with the error case. Task failure works nicely.
>
> 2. Sometimes we do want to do something different on invalid input, so returning an `Option<T>` works best.
>
> And so we end up with both `from_utf8` and `from_utf8`. This particular case is worse because we also have `from_utf8_owned` and `from_utf8_owned_opt`, to cover everything.
>
> Multiplying names like this is just not good design. I’d like to reduce this pattern.
>
> Getting behavior 1. when you have 2. is easy: just call `.unwrap()` on the Option. I think we should rename every `foo_opt()` function or method to just `foo`, remove the old `foo()` behavior, and tell people (through documentation) to use `foo().unwrap()` if they want it back?
>
> The downsides are that unwrap is more verbose and gives less helpful error messages on task failure. But I think it’s worth it.


The email discussion has gone around long enough. Let’s discuss a concrete proposal. For the following functions or methods, I removed `foo` (that caused task failure) and renamed `foo_opt` (that returns `Option`) to just `foo`.

Vector methods:

* `get_opt` (rename only, `get` did not exist as it would have been just `[]`)
* `head_opt`
* `last_opt`
* `pop_opt`
* `shift_opt`
* `remove_opt`

`std::path::BytesContainer` method:

* `container_as_str_opt`

`std::str` functions:

* `from_utf8_opt`
* `from_utf8_owned_opt` (also remove the now unused `not_utf8` condition)

Is there something else that should recieve the same treatement?

I did not rename `recv_opt` on channels based on @brson’s [feedback](https://mail.mozilla.org/pipermail/rust-dev/2013-December/007270.html).

Feel free to pick only some of these commits.
@bors bors closed this Jan 22, 2014
@SimonSapin SimonSapin deleted the foo-vs-foo_opt branch June 4, 2014 23:23
flip1995 pushed a commit to flip1995/rust that referenced this pull request Jul 14, 2023
[significant_drop_tightening] Fix rust-lang#11128

Fix rust-lang#11128

```
changelog: [`significant_drop_tightening`]: Consider manual alias of the `drop` function.
```
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

Successfully merging this pull request may close these issues.

5 participants