测试一个Dart值是否实际上是一个函数? [英] Test if a Dart value is actually a function?

查看:390
本文介绍了测试一个Dart值是否实际上是一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以测试某个值是否为可调用的函数?我可以容易地测试null,但之后,我不知道如何确保传入的参数实际上是一个函数?

  void myMethod(funcParam)
{
if(funcParam!= null)
{
/ *如何测试funcParam实际上是一个函数称为? * /
funcParam();
}
}


解决方案



void myMethod(funcParam){
if(funcParam is Function){
funcParam();
}
}

当然,调用 funcParams()仅在参数列表匹配 - 是函数时不起作用。如果涉及参数,可以使用 typedef 确保this。

  typedef void MyExpectedFunction(int someInt,String someString); 

void myMethod(MyExpectedFunction funcParam,int intParam,String stringParam){
if(funcParam is MyExpectedFunction){
funcParam(intParam,stringParam);
}
}


Is it possible to test if a value is a function that can be called? I can test for null easily but after that I have no idea how to ensure the parameter passed in is actually a function?

void myMethod(funcParam)
{
   if (funcParam != null)
   {
       /* How to test if funcParam is actually a function that can be called? */
       funcParam();
   }
}

解决方案

void myMethod(funcParam) {
    if(funcParam is Function) {
        funcParam();
    }
}

Of course, the call to funcParams() only works if the parameter list matches - is Function doesn't check for that. If there are parameters involved, one can use typedefs to ensure this.

typedef void MyExpectedFunction(int someInt, String someString);

void myMethod(MyExpectedFunction funcParam, int intParam, String stringParam) {
    if(funcParam is MyExpectedFunction) {
        funcParam(intParam, stringParam);
    }
}

这篇关于测试一个Dart值是否实际上是一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆