mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-15 07:06:11 -04:00
ExceptionOr<T>::ok() returns true if the value is set. This can be misleading
for boolean types.
ExceptionOr<bool> result = SomeFunction();
if (result.ok()) {
if (result.result() {
// Case 1: SomeFunction returned true
} else {
// Case 2: SomeFunction returned false
} else {
// Case 3: SomeFunction returned an exception
}
It's easy to overlook case 2.
The specialized ExceptionOr<bool>::ok() returns false in Case 2, which allows
us to write:
if (result) {
// SomeFunction returned true
} else {
// SomeFunction returned false or an exception
// result.GetException() can be used if we need to handle different exceptions
// differenty
}
PiperOrigin-RevId: 544715641