如何在Dart中传递常见的论点? [英] How to pass common arguments in Dart?

查看:68
本文介绍了如何在Dart中传递常见的论点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,我有:

callback.orderBy('foo', reverse: true).localToThis();
callback.orderBy('foo', reverse: true);

您可以在两个回调函数中看到,我传递的参数相同,

You can see in both callbacks, I am passing same arguments,

'foo', reverse: true

有什么方法可以使它们成为某种东西(变量或方法),以便可以使用类似这样的东西:

Is there any way I can make them something (a variable or a method) so that I can use something like this:

callback.orderBy(common).localToThis();
callback.orderBy(common);


推荐答案


callback.orderBy('foo', reverse: true).localToThis();
callback.orderBy('foo', reverse: true);


如果您要使用一组相同的函数来调用相同的函数参数,如果函数没有副作用,则最好保存结果并避免再次调用该函数。

If you're calling the same function with the same set of arguments, if the function has no side-effects, you might as well just save the result and avoid calling the function again.

如果函数确实具有副作用或如果要仅使用某些参数调用该函数,则可以使用部分函数应用程序绑定参数:

If the function does have side-effects or if you want to call the function with only some of its arguments the same, you could use a partial function application to bind arguments:

final orderBy = () => callback.orderBy('foo', reverse: true);
orderBy().localToThis();
orderBy();

这篇关于如何在Dart中传递常见的论点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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