operator ~ method

T operator ~()

Shortcut to call Option.unwrap().

Warning: This is an unsafe operation. An OptionError will be thrown if this operator is used on a None value. You can take advantage of this safely via propagateOption/propagateOptionAsync and their respective shortcuts (OptionPropagateShortcut.~ / OptionPropagateShortcutAsync.~).

This is as close to analagous to Rust's ? postfix operator for Option 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 Option.unwrap().

var foo = Some(1);
var bar = Some(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 opt = Some(1);

print((~opt).toString());

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

var opt = Some(1);

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

See also: Rust: Option::unwrap()

Implementation

T operator ~() => unwrap();