5 ms·
What is a "cascaded nil-coalescing operator" ?
by jtchang 7y ago
What is a "cascaded nil-coalescing operator" ?
- omaranto 7y agoSomething like a.?b.?c which is syntactic sugar for: a && a.b && a.b.c (with a short-circuiting &&.)
- rimliu 7y agoThis is just optionals chaining, not nil coalescing.
- omaranto 7y agoThanks for correcting me, I actually wasn't aware of nil coalescing in Swift and guessed it meant the same thing as optional chaining. The nil coalescing operator seems to correspond to Haskell maybe function, that is, a ?? b is computed as follows: if a is nil, the result is b; otherwise a must wrap some actual value c and the result is c. Is that the one you had in mind?
- dtech 7y agoThis is Javascript/Typescript only, and not really correct. a?.b a && a.b will behave differently if a is false or 0 or an otherwise "falsy" value. It's identical to something similar though (a !== undefined && a !== null) ? a : b
- omaranto 7y agoThat's right, of course. I was just trying to convey the idea. But details matter of course. I was thinking of variables that either contained nil or contained an object with the appropriate field. If that precondition isn't meant you need more robust code like yours. But also, as another commenter pointed out, there is a distinction between "optional chaining" and "nil coalescing" and I explained (roughly) what optional chaining is.
- amflare 7y agoWell a nil-coalescing operator is: a ?? b Where this is shorthand for: a != nil ? a! : b idk what cascaded means, but it sounds like a ternary operation from hell.
- ChrisMarshallNY 7y agoThink the ternary operator, but worse. In Swift, this is expressed by "??". It means "If the previous test returns nil, then execute what is after the ??". For example, if you have a concrete Int, but the source might be an optional, then you could do something like this: let a = b ?? 0 That means that if b is nil, then set a to 0. Otherwise, set it to whatever value b has. You can chain these, like so: let b: Int? = <something from somewhere> let c: Int? = <something from somewhere> let a: Int = b ?? c ?? 0 So you have a couple of optional values coming in, but they could be nil, so you chain them. You can have a lot more going on in the handlers than simple assignments, but this gives you an idea.
- accatyyc 7y agoIn Objective-C (and some other languages with ternary operators) the same can be written like this: int a = b ?: c ?: 0
- ChrisMarshallNY 7y agoYup. Personally, I like the ternary operator, and I also like the nil-coalescing operator, but I'm quite aware that they can produce difficult-to-maintain code, so I'm careful with them. I have gone and done some silly stuff with both, but then, I realized that I was leaving unmaintainable code. If you use godbolt.org, you can actually see what code is produced by the source.
- messe 7y agoThe second conditional is unneeded as the 0 will only be returned if c evaluates to 0, so it can equally well be written int a = b ?: c;
- WWLink 7y agoThat one's straight up called the Elvis Operator. I hate it when things like this get names that everyone's supposed to know and it's considered a mark against you if you've never heard it before, but I do think the name Elvis Operator is hilarious. :D
- AgloeDreams 7y agoI'm not sure how it looks in ObjC but in Typescript you could write something like: let studentScoreFormatted: string = student.formattedScore ?? 'Student has not taken this test yet.'; Whereas if the score is null it will return the value after `??`. It is awfully helpful for ensuring you handle error states in displaying API values. Additionally if you want this behavior but when the value is falsey, then you can use `||` instead.
- jamil7 7y agoDo you mean Swift? I didn't think Typescript had something like this yet.
- h0h0h0h0111 7y agoNot sure about OP, but this is coming in Typescript 3.7 I believe. Currently you can use `||`, but as mentioned above it will also catch false, 0, "", etc
- AgloeDreams 7y agoBingo. TS 3.7
- leshow 7y ago?? doesn't exist in typescript yet, but there is a tc39 proposal to add it to js proper
- AgloeDreams 7y agoTS 3.7 adds it.
- rimliu 7y agoA bunch of double question marks with some optionals in between would be my guess. optional1 ?? optional2 ?? optional3 ?? ifeverythingelsefailsvalue
- whatshisface 7y agoIt's Swift's equivalent of Rust's `unwrap_or`, or the trick in python where you write `None or "Hi!" == "Hi!"`.
- JKCalhoun 7y agoSomeone that works in a Matryoshka doll factory? Just a guess. I'm not a CS major either.
- pmhpereira 7y agoa = b ?: c; if b is not nil, then a = b else a = c
- emilfihlman 7y agoApparently it functions a bit similar to the javascript || assignment. https://stackoverflow.com/questions/2100758/javascript-or-variable-assignment-explanation https://stackoverflow.com/questions/2100758/javascript-or-va...
- mattz0rt 7y agoPython equivalent: return a or b