Skip to content

Instantly share code, notes, and snippets.

@leonsenft
Last active June 28, 2018 06:20
Show Gist options
  • Save leonsenft/396f0f9b95e4bee49fb13fab97fbe7d3 to your computer and use it in GitHub Desktop.
Save leonsenft/396f0f9b95e4bee49fb13fab97fbe7d3 to your computer and use it in GitHub Desktop.
TypeError due to lack of inference
typedef PathFunction = String Function([List<String> segment]);
PathFunction createPathFunction() {
// The type of this default list is not inferred from context.
// Typing the LHS or applying type arguments to the RHS fixes this.
return ([args = const []]) {
return '/${args.join('/')}';
};
}
void main() {
final toPath = createPathFunction();
// Passing a list with the correct type works.
print(toPath(['hello', 'world']));
// Even passing a list with no element works, so inference from the
// closure signature works.
print(toPath([]));
// Omitting the list argument causes a runtime error.
print(toPath());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment