In Dart, functions are first class. The documentation tells us that you can create a variable that is a function like this:
var loudify = (msg) => '!!! ${msg.toUpperCase()} !!!';
to create a function named "loudify" that takes a String and returns another String.
If I don't know beforehand which function I want to assign to the variable, I can do it like this:
// define the variable
var loudify;
// later on set the variable
loudify = (msg) => '!!! ${msg.toUpperCase()} !!!';
But how do I use optional typing so that I know later on that the variable is of type "function" and has input String and output String? I would suggest something like this, but this doesn't work (Dart editor tells me "undefined class 'function'"):
// this gives a syntax error in the Dart editor
function<String, String> loudify;
So what is the proper syntax here?
Regards,
Hendrik
You can use typedef
typedef String OneString(String x);
typedef String OneStringAndInt(String x, int y);
void main() {
//var f = (String x) => '$x';
var f = (String x, int y) => '$x$y';
if(f is OneString) {
print(f('bla'));
} else {
print(f('bla', 10));
}
}