Somewhat of practical programming – or – extensions are cool

Infrequently, it is not conceivable to make use of `?.` or `??` to optionally name a serve as and one has to jot down an specific check for `null`. This makes me need to upload a `toInt` strategy to `String` however on the other hand, I do not upload random code to random categories, so:

int? foo(String? string) =>
string != null ? int.parse(string) : null;

It’s value so as to add a `map` means (in most cases referred to as a _functor_ and I am the usage of the similar identify as Swift makes use of right here) to any object like so?

extension<T extends Object> on T? {
U? map<U>(U Serve as(T worth) toElement) =>
this != null ? toElement(this!) : null;

Then, it is conceivable to make use of a cleaner expression:

int? foo(String? string) =>;

If I need to supply a default worth, I will be able to use `??` once more:

int foo(String? string) => ?? -1;

View Reddit by means of eibaanView Supply

You may also like...

WP2Social Auto Publish Powered By :