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

Optional.require(String)? #42

Closed
Tracked by #40
Sparky983 opened this issue Sep 19, 2023 · 2 comments · Fixed by #49
Closed
Tracked by #40

Optional.require(String)? #42

Sparky983 opened this issue Sep 19, 2023 · 2 comments · Fixed by #49
Assignees
Labels
enhancement New feature or request

Comments

@Sparky983
Copy link
Owner

Sparky983 commented Sep 19, 2023

Example usage:

/*
 * In reality this would be something where we don't 
 * know the value, but we are sure the optional has 
 * a value, rather than here where we know the value.
 */

class MyValueHolder {
  Optional<String> value = Optional.absent();
  void set(String s) { value = Optional.present(s); }
  Optional<String> get() { return value; }
}

MyValueHolder holder = ...;
holder.set("abc");
String s = holder.value().require("holder to have a value");
@Sparky983 Sparky983 mentioned this issue Sep 19, 2023
3 tasks
@Sparky983
Copy link
Owner Author

As mentioned in #39, here are some alternative names:

  • Optional.orThrow(String)
  • Optional.expect(String)

@Sparky983 Sparky983 self-assigned this Sep 30, 2023
@Sparky983
Copy link
Owner Author

Will be introduced under the name expect as an experimental API.

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

Successfully merging a pull request may close this issue.

1 participant