Hello Mat,
thank you for the reply. I know about int.parse(b, onError: (_) => null);
but it gives me an exception, try
String b = null;
int a = int.parse(b, onError: (_) => null);
It will print (I'm using dart-sdk-1.24.2)
Unhandled exception:
Invalid argument(s): The source must not be null
#0 int.parse (dart:core-patch/integers_patch.dart:44)
So I have to use
int a = b == null ? null : int.parse(b, onError: (_) => null);
where b == null ? null handles b is null and onError handles incorrect
format. Which is really ugly.
If you say there will be a = int.tryParse(b); which will handle null and
incorrect format, I'm fine with it and I'll wait for it a is looks promissing.
Regards,
Martin
Dne 1.12.2017 v 19:05 Matthew Butler napsal(a):
> Based on the information that was presented about Library
> cleanups,
https://github.com/dart-lang/sdk/blob/master/docs/newsletter/lib/lib.md
> this will be a redundant request with the new parsing, and in particular
> with the new `tryParse` parameters.
>
> Currently in your examples, this is what I would actually do:
>
> int a = int.parse(b, onError: (_) => null);
> Which handles not only null, but also invalid formats for b (eg b ==
> "fiftytwo").
>
> The proposed library changes will allow:
> int a = int.tryParse(b);
>
> This will automatically assign null to a, if b fails (because it's null or
> otherwise invalid parsing).
>
> Outside of that, I don't see a lot of situations where you would want to
> prefer null?
>
> Respectfully,
>
> Matt
>
> On Friday, December 1, 2017 at 1:44:32 PM UTC-4, Martin Edlman wrote:
>
> Hello,
>
> after using Dart for few moths I found it very efficient, null-aware
> operators are great shorthands. What I'm missing is negate of ??
> operator. In following examples I use *?!* but it could be *!?* or
> anything else.
> If
> int a = b ?? c;
> means if b is not null assign b to a, otherwise assign c to a, there
> should be a negate operation
> int a = b *?!* int.parse(b);
> which means if bis null, assign nullto a, otherwise parse non-null bto
> int and assign it to a.
>
> There are lot of cases where this could save lot of code. E.g.
> void doSomething({int repeat: 5}) { ... }
> int a = b *?!* int.parse(b);
> doSomething(repeat: c *?!* c + 1);
>
> Now it must be written as
> int a = b == null ? null : int.parse(b);
> int x = int.parse(b ?? '0'); // will assign 0 instead of null which
> might not be desired
> doSomething(repeat: c == null ? null : c + 1);
>
> I hope you'll find this idea interesting and that it will find a way to
> Dart.
>
> Regards, Martin
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Dart Core Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
core-dev+u...@dartlang.org
> <mailto:
core-dev+u...@dartlang.org>.