Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you determine if a variable is "callable" in dart?

Tags:

dart

I am doing a small experiment in dart and I couldn't find a way to determine if a variable is "callable" without explicitly checking for each type (String, int, bool, ect) and guessing that it was callable if it was none of those. I also experimented with a try/catch which to me just seems wrong.

Whats the right way or at least the best way to make that determination?

Here is an example I did to show what I am trying to accomplish: https://gist.github.com/digitalfiz/3f431dc07ca761389062

like image 267
DigitalFiz Avatar asked Mar 20 '23 10:03

DigitalFiz


1 Answers

Use this function:

  bool isCallable(v) => v is Function;

Usage Examples:

class Callable {
  call() => 42;
}

void main() {
  var foo = () => 42;
  var bar = new Callable();
  var baz = 42;

  print(isCallable(foo)); //true
  print(isCallable(bar)); //true
  print(isCallable(baz)); //false
}
like image 187
JAre Avatar answered Mar 22 '23 04:03

JAre