Last active
June 28, 2018 06:20
-
-
Save leonsenft/396f0f9b95e4bee49fb13fab97fbe7d3 to your computer and use it in GitHub Desktop.
TypeError due to lack of inference
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
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