Created
January 11, 2023 18:05
-
-
Save tvolkert/9c287e5cf29c5dcef67baf3ccc5af5dc to your computer and use it in GitHub Desktop.
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() { | |
apiMethod('foo'); | |
apiMethod('foo', (value) => print('Hello $value')); | |
} | |
typedef Callback = void Function(String value); | |
void apiMethod(String value, [Callback? callback]) { | |
// What I would love to do instead of this if/else is something like: | |
// `backingMethod(value, callback ?? default)`, where `default` is a | |
// reserved word. | |
if (callback == null) { | |
backingMethod(value); | |
} else { | |
backingMethod(value, callback); | |
} | |
} | |
// Imagine these methods are in another package, so _default is not | |
// visible to the frontend API. | |
void backingMethod(String value, [Callback callback = _default]) { | |
callback(value); | |
} | |
void _default(String value) { | |
print('value is $value'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment