Proposal for null-aware negate operator ?!

161 views
Skip to first unread message

Martin Edlman

unread,
Dec 1, 2017, 12:44:32 PM12/1/17
to Dart Core Development
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 b is null, assign null to a, otherwise parse non-null b to 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


Matthew Butler

unread,
Dec 1, 2017, 1:05:35 PM12/1/17
to Dart Core Development
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
Message has been deleted

Martin Edlman

unread,
Dec 1, 2017, 7:08:21 PM12/1/17
to Matthew Butler, Dart Core Development
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>.


Rockvole

unread,
May 29, 2018, 12:14:17 PM5/29/18
to Dart Core Development
I don't believe tryParse() deals with null as described above.

print(int.tryParse(null).toString());

Unhandled exception:
Invalid argument(s): The source must not be null
#0      int.tryParse (dart:core-patch/dart:core/integers_patch.dart:112)
#1      main (file:///work/projects/dart/kbml_parser/main_test.dart:10:13)
#2      _startIsolate.<anonymous closure> (dart:isolate-patch/dart:isolate/isolate_patch.dart:279)
#3      _RawReceivePortImpl._handleMessage (dart:isolate-patch/dart:isolate/isolate_patch.dart:165)

Rockvole

unread,
May 29, 2018, 12:21:04 PM5/29/18
to Dart Core Development
Reply all
Reply to author
Forward
0 new messages