You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
final data =callApi((apiService) => apiService.getData());
It would be great to be able to create the (apiService) => apiService.getData() closure more concisely.
As static functions and instance functions cannot share names, the instance function could be automatically added as a static function, like so:
classMyClass {
voidmyInstanceFunction(Object a, Object b, Object c) {}
}
MyClass.myInstanceFunction; // (myObject, a, b, c) => myObject.myInstanceFunction(a, b, c);
That syntax may be a little confusing, though, as it adds a lot of duplicate functions - something like MyClass.methods.myInstanceFunction could be done instead.
The text was updated successfully, but these errors were encountered:
Maybe get some inspiration from Java with :: so we can do: callApi(ApiService::getData()) where the :: operator will be the same as creating: (ApiService apiService) => apiService.getData().
An argument against this, is that we can do this syntax shorter by just not provide a good name (or type) for the variable. So we could just shorten it into: callApi((s) => s.getData());. But the cost is readability.
An argument against this, is that we can do this syntax shorter by just not provide a good name (or type) for the variable. So we could just shorten it into: callApi((s) => s.getData());. But the cost is readability.
Another potential benefit to adding this language feature (though I'm not sure is technically possible) is for these generated closures to be used in const expressions. This is currently not possible with a closure literal.
It's quite common to use a closure that takes a single parameter, and calls a single function on it.
For example, this pattern can be used to ensure that an API is only accessed in a safe manner:
It would be great to be able to create the
(apiService) => apiService.getData()
closure more concisely.As static functions and instance functions cannot share names, the instance function could be automatically added as a static function, like so:
That syntax may be a little confusing, though, as it adds a lot of duplicate functions - something like
MyClass.methods.myInstanceFunction
could be done instead.The text was updated successfully, but these errors were encountered: