Re: [Vala] Null Coalescing Assignment Operator
- From: Al Thomas <astavale yahoo co uk>
- To: "vala-list gnome org" <vala-list gnome org>
- Subject: Re: [Vala] Null Coalescing Assignment Operator
- Date: Sun, 28 May 2017 17:04:18 +0000 (UTC)
From: Guillaume Poirier-Morency <guillaumepoiriermorency gmail com>
Sent: Sunday, 28 May 2017, 16:59
Subject: Re: [Vala] Null Coalescing Assignment Operator
Le dimanche 28 mai 2017 à 10:20 +0200, Ulink a écrit :
thing = thing ? create ();
I think you meant
thing = thing ?? create ();
Yeah, my mistake! Evan actually pointed that out first on IRC.
Edward seems lazy and don't want to write "thing" two times ;-)
What I think we need is an Elvis accessor to turn:
C? c = null;
var a = thing_a ();
if (a != null) {
var b = thing_b ();
if (b != null) {
c = thing_c ();
}
}
into:
var c = thing_a ()?.thing_b ()?.thing_c ();
I think it's better just to give your variables sensible defaults and
avoid the whole null things as much as possible. So use an empty string
for a customer without an email address and so on. Look up the computer
scientist Tony Hoare and why he thinks nulls were his billion dollar
mistake! :)
I think the ?. notation is often referred to as the safe navigation operator.
As you say there is also the Elvis operator, ?:, when used as a binary
operator and ? : used as a ternary operator. And of course ?? used as a
null coalescing operator. If programmers aren't using this kind of symbolic
logic regularly it can be hard to recall precisely what is supposed to be
happening and read existing code. It's kind of like the combination of key
presses needed to exit vim when it suddenly pops up on a newly installed
Unix system. Take a look at this for a mildly amusing read:
https://stackoverflow.blog/2017/05/23/stack-overflow-helping-one-million-developers-exit-vim/
As far as the null safe navigation operator goes here are some interesting
counterpoints:
http://enterprisecraftsmanship.com/2015/05/11/3-misused-of-operator-in-c-6/
Al
[
Date Prev][Date Next] [
Thread Prev][Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]