News for hackers. Stuff that matters.
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.)