空感知函数调用? [英] Null-aware function call?
本文介绍了空感知函数调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Dart有一些可以感知空值的运算符,即可以执行
Dart has some null-aware operators, i.e. it is possible to do
var obj;
obj?.foo(); // foo is only called if obj != null.
对于存储或传递给变量的函数,这也是可能的吗?通常的模式是
Is this also possible for functions that are stored or passed to variables? The usual pattern is
typedef void SomeFunc();
void foo(SomeFunc f) {
if (f != null) f();
}
在这里有一些可以识别空值的调用会很好,例如 f?()
。
It would be nice to have some null-aware calling here, like f?()
. Is there anything we can use to not litter the code with null checks for those callbacks?
推荐答案
形成文档:
Dart是一种真正的面向对象的语言,因此,即使函数也是对象,并且具有函数类型。
Dart is a true object-oriented language, so even functions are objects and have a type, Function.
null知道 ??。
运算符,用于函数对象的调用
方法:
Apply the null aware ?.
operator to the call
method of function objects:
typedef void SomeFunc();
SomeFunc f = null;
f?.call();
这篇关于空感知函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文