Created
December 29, 2019 20:01
-
-
Save passsy/3c884a82f6937e90bc132626e63321b1 to your computer and use it in GitHub Desktop.
Comparison between ?? and a orDefault method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
final String name = "Adam"; | |
print("=== ?? ==="); | |
// does not execute defaultValue() | |
print(name ?? defaultValue()); | |
print("=== orDefault ==="); | |
// executes defaultValue() | |
print(name.orDefault(defaultValue())); | |
} | |
String defaultValue() { | |
print("execute defaultValue()"); | |
return "Charles"; | |
} | |
extension Ext<T> on T { | |
T orDefault(T defaultValue) { | |
if (this == null) return defaultValue; | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment