News for hackers. Stuff that matters.
Re:Apple Developer Documentation Is Missing by jtchang on Monday October 28, 2019 @10:39AM (#21377384)
What is a "cascaded nil-coalescing operator" ?
Re:Apple Developer Documentation Is Missing by ChrisMarshallNY on Monday October 28, 2019 @10:45AM (#21377439)
Think 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.

Re:Apple Developer Documentation Is Missing by eugeniub on Monday October 28, 2019 @11:11AM (#21377662)
Strongly disagree with the "but worse" characteristic. As soon as you learn what "??" does, it's actually an improvement in readability in many cases. For example:

  nameLabel.text = user?.name ?? "Guest"
This, as opposed to:

  if let name = user?.name {
    nameLabel.text = name
  } else {
    nameLabel.text = "Guest"
  }
Re:Apple Developer Documentation Is Missing by bdisnainc on Monday October 28, 2019 @11:48AM (#21377990)
That is because you apply pattern matching when it is not really useful here, on top of avoiding a simple ternary operator.

In some languages, you simply write:

    if (user)
      nameLabel.text = user.name;
    else
      nameLabel.text = "Guest";
Or:

    nameLabel.text = user.name if user else "Guest"

Or anything along those lines. Concise, clear, no need for an extra operator.
Re:Apple Developer Documentation Is Missing by gnud on Monday October 28, 2019 @11:06AM (#21377608)
... and this is so wonderful! It's in no way worse than the ternary operator. You punt to a default value really easily, without having 20 lines of `if (a.this == null) a.this == 0; if(a.that == null)a.that == ""` or similar.

When constructing objects "in-line" in c# I actually used to see the ternary operator quite a lot, as in

  new SomeClass() {
    Val = valSource == null? SomeClass.DefaultVal : valSource
  }
The coalesce operator is way better. Except it can't be used in LINQ projections.
Re:Apple Developer Documentation Is Missing by Razengan on Monday October 28, 2019 @11:56AM (#21378068)
> Think the ternary operator, but worse.

How the hell is it worse? It lets me write things like

    toolbar.visibility = userSettings.showToolbar ?? defaultSettings.showToolbar

    someOption = localPreference ?? onlinePreference ?? defaultPreference
which is a lot better for the coder and a reader than any other alternative I can currently think of.
Re:Apple Developer Documentation Is Missing by Razengan on Monday October 28, 2019 @02:50PM (#21380220)
Think I like others may have read that wrong and it was meant as a joke.
Re:Apple Developer Documentation Is Missing by crusso on Monday October 28, 2019 @11:03AM (#21377589)
"Think the ternary operator, but worse"

Giving the developer succinct ways to prevent program failure from unexpectedly undefined values is a feature.

[dead]
Re:Apple Developer Documentation Is Missing by eugeniub on Monday October 28, 2019 @11:14AM (#21377695)
> Most languages have had this since the 1960s.

> It's called the "if" clause.

If we got rid of everything we don't absolutely need in programming, we would have to get rid of 98% of Swift syntax, and probably every modern programming language including Swift.

Re:Apple Developer Documentation Is Missing by ChrisMarshallNY on Monday October 28, 2019 @11:25AM (#21377772)
Yes, but I LIKE Swift, because of all that sugar.

I started off with machine language, in embedded systems. You can't get much more "raw" than that.

I have been writing Swift exclusively for a long time. I speak it without an accent. It's my beauty.

But I also know that you can have so much sugar you fall into a diabetic coma.

Re:Apple Developer Documentation Is Missing by FpUser on Monday October 28, 2019 @11:51AM (#21378019)
>But I also know that you can have so much sugar you fall into a diabetic coma.

Exactly my feelings. Same thing here started from machine code and now doing everything else including kitchen sink

Re:Apple Developer Documentation Is Missing by megous on Monday October 28, 2019 @01:27PM (#21379221)
Did you guys really start from machine code or do you mean assembler?
Re:Apple Developer Documentation Is Missing by FpUser on Monday October 28, 2019 @08:30PM (#21383238)
Machine code. Was actually typing it on big roll of paper and then crawling around it pretending to be a debugger
Re:Apple Developer Documentation Is Missing by ChrisMarshallNY on Monday October 28, 2019 @02:58PM (#21380286)
Machine. Hex keypad and a lot of looking up hex codes for commands.

Type in a bunch of numbers, then set the PC to 0.

Assembler was great after that.

I do not remember that stuff fondly. It sucked.

Re:Apple Developer Documentation Is Missing by leshow on Monday October 28, 2019 @12:05PM (#21378162)
> It's called the "if" clause.

"If" is often a statement, not an expression, so they aren't equivalent in a lot of cases. I'm not sure if this is the case in Swift, but I'm fairly certain that Swift's '?' operator also does unwrapping and propagation, similar to '?' in Rust.

Re:Apple Developer Documentation Is Missing by accatyyc on Monday October 28, 2019 @11:01AM (#21377565)
In Objective-C (and some other languages with ternary operators) the same can be written like this:

    int a = b ?: c ?: 0
Re:Apple Developer Documentation Is Missing by WWLink on Monday October 28, 2019 @11:36AM (#21377874)
That 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

Re:Apple Developer Documentation Is Missing by toxik on Monday October 28, 2019 @11:58AM (#21378083)
I was wondering why the funny name, and

> The name "Elvis operator" refers to the fact that when its common notation, ?:, is viewed sideways, it resembles an emoticon of Elvis Presley.

https://en.wikipedia.org/wiki/Elvis_operator

Re:Apple Developer Documentation Is Missing by pflats on Monday October 28, 2019 @01:44PM (#21379453)
Also, a lot of other languages (Python, Ruby, JS, etc.) can do the same thing with their OR operator:

    b = list.next.to_s || "Reached end of list."
If list.next.to_s is non-nil, then the OR statement is short-circuited and list.next.to_s will be assigned to b. If b is nil and "Reached end of list." is non-nil, then OR statement will return the string literal to be assigned to b instead. Thus,

    a = b || c || 0
would be the equivalent for those languages.

edit: One BIG downside to this I forgot to mention at first: If nil and a false value are both possible for a variable, then this construction can betray you and break your heart.

Re:Apple Developer Documentation Is Missing by ChrisMarshallNY on Monday October 28, 2019 @11:04AM (#21377594)
Yup.

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.

Re:Apple Developer Documentation Is Missing by messe on Monday October 28, 2019 @11:15AM (#21377705)
The 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;
Re:Apple Developer Documentation Is Missing by DerekL on Monday October 28, 2019 @02:07PM (#21379734)
That's not specific to Objective-C, it's a GCC extension to the C language.
Re:Apple Developer Documentation Is Missing by saagarjha on Monday October 28, 2019 @07:03PM (#21382703)
(Which Clang happens to support, hence it working here.)
Re:Apple Developer Documentation Is Missing by AgloeDreams on Monday October 28, 2019 @10:46AM (#21377453)
I'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.

Re:Apple Developer Documentation Is Missing by leshow on Monday October 28, 2019 @12:07PM (#21378190)
?? doesn't exist in typescript yet, but there is a tc39 proposal to add it to js proper
Re:Apple Developer Documentation Is Missing by AgloeDreams on Monday October 28, 2019 @02:02PM (#21379673)
TS 3.7 adds it.
Re:Apple Developer Documentation Is Missing by jamil7 on Monday October 28, 2019 @11:55AM (#21378057)
Do you mean Swift? I didn't think Typescript had something like this yet.
Re:Apple Developer Documentation Is Missing by h0h0h0h0111 on Monday October 28, 2019 @12:07PM (#21378189)
Not 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
Re:Apple Developer Documentation Is Missing by AgloeDreams on Monday October 28, 2019 @02:17PM (#21379876)
Bingo. TS 3.7
Re:Apple Developer Documentation Is Missing by omaranto on Monday October 28, 2019 @10:43AM (#21377419)
Something like a.?b.?c which is syntactic sugar for:

    a && a.b && a.b.c
(with a short-circuiting &&.)
Re:Apple Developer Documentation Is Missing by dtech on Monday October 28, 2019 @10:56AM (#21377522)
This 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
Re:Apple Developer Documentation Is Missing by omaranto on Monday October 28, 2019 @02:16PM (#21379855)
That'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.

Re:Apple Developer Documentation Is Missing by rimliu on Monday October 28, 2019 @10:47AM (#21377463)
This is just optionals chaining, not nil coalescing.
Re:Apple Developer Documentation Is Missing by omaranto on Monday October 28, 2019 @02:18PM (#21379882)
Thanks 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?

Re:Apple Developer Documentation Is Missing by amflare on Monday October 28, 2019 @10:43AM (#21377424)
Well 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.
Re:Apple Developer Documentation Is Missing by whatshisface on Monday October 28, 2019 @10:46AM (#21377456)
It's Swift's equivalent of Rust's `unwrap_or`, or the trick in python where you write `None or "Hi!" == "Hi!"`.
Re:Apple Developer Documentation Is Missing by emilfihlman on Monday October 28, 2019 @11:05AM (#21377605)
Apparently it functions a bit similar to the javascript || assignment.

https://stackoverflow.com/questions/2100758/javascript-or-va...

Re:Apple Developer Documentation Is Missing by rimliu on Monday October 28, 2019 @10:46AM (#21377455)
A bunch of double question marks with some optionals in between would be my guess.

    optional1 ?? optional2 ?? optional3 ?? ifeverythingelsefailsvalue
Re:Apple Developer Documentation Is Missing by pmhpereira on Monday October 28, 2019 @10:52AM (#21377497)
a = b ?: c;

if b is not nil, then a = b else a = c

Re:Apple Developer Documentation Is Missing by mattz0rt on Monday October 28, 2019 @11:39AM (#21377916)
Python equivalent:

  return a or b