Skip to content

Instantly share code, notes, and snippets.

@tvolkert
Created January 11, 2023 18:05
Show Gist options
  • Save tvolkert/9c287e5cf29c5dcef67baf3ccc5af5dc to your computer and use it in GitHub Desktop.
Save tvolkert/9c287e5cf29c5dcef67baf3ccc5af5dc to your computer and use it in GitHub Desktop.
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