operator ~ method

T operator ~()

Shortcut to call Result.unwrap().

Warning: This is an unsafe operation. A ResultError will be thrown if this operator is used on a None value. You can take advantage of this safely via propagateResult/propagateResultAsync and their respective shortcuts (ResultPropagateShortcut.~ / ResultPropagateShortcutAsync.~).

This is as close to analagous to Rust's ? postfix operator for Result values as Dart can manage. There are no overrideable postfix operators in Dart, sadly, so this won't be as ergonomic as Rust but it's still nicer than calling Result.unwrap().

var foo = Ok(1);
var bar = Ok(2);

print(~foo + ~bar); // prints: 3

Note: if you need to access fields or methods on the held value when using ~, you'll need to use parentheses like so:

var res = Ok(1);

print((~res).toString());

Additionally, If you need to perform a bitwise NOT on the held value of a Result, you have a few choices:

var res = Ok(1);

print(~(~res)); // prints: -2
print(~~res); // prints: -2
print(~res.unwrap()); // prints: -2;

See also: Rust: Result::unwrap()

Implementation

T operator ~() => unwrap();