inko icon indicating copy to clipboard operation
inko copied to clipboard

Consider renaming "unwrap" methods to "get", and rename "expect" to something better

Open yorickpeterse opened this issue 2 years ago • 0 comments

Methods such as unwrap, unwrap_or, unwrap_or_else aren't very descriptive if you've not used Rust before, as it's not clear what "unwrapping" means.

Similarly, expect is poorly named and pushes users in the direction of writing error messages that say what should've happened rather than what happened, with the problem being that it's sometimes really difficult to write such error messages.

I'm now thinking of renaming such methods as follows:

  • unwrap becomes get
  • unwrap_or becomes or
  • unwrap_or_else becomes or_else
  • expect becomes or_panic, or_fail, or something along those lines; not sure yet

Another issue with expect is that even if the thrown error value can be formatted, expect ignores it so you lose information about the error. To handle that we'd probably need a separate method implemented along the lines of the following:

impl Result if E: ToString {
  fn pub require -> T {
    match self {
      case Ok(v) -> v
      case Error(e) -> panic(e.to_string)
    }
  }
}

This however results in an inconsistency between users using require and whatever we rename expect to, which can be annoying.

yorickpeterse avatar Nov 26 '23 15:11 yorickpeterse