Reputation: 28906
Say, I have:
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);
Upvotes: 0
Views: 443
Reputation: 89995
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();
Upvotes: 1
Reputation: 329
You can create a class, containing fields you want to be common
class OrderConfiguration {
final String orderBy;
final bool reverse;
OrderConfiguration(this.orderBy, {reverse = false});
}
and then accept an instance of OrderConfiguration
as an argument to orderBy
But you can do this only for your own functions, for functions that weren't written by you, you could make a wrapper function. (May be this is a better option for your function too)
void orderCallback(callback) {
callback.orderBy('foo', reverse: true);
}
or if orderBy
returns a value
Type orderCallback(callback) {
return callback.orderBy('foo', reverse: true);
}
Upvotes: 1