6 ms·
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 ??". F
by ChrisMarshallNY 7y ago
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.
- 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
- toxik 7y agoI 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 https://en.wikipedia.org/wiki/Elvis_operator
- deleted 7y ago[deleted]
- pflats 7y agoAlso, 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.
- DerekL 7y agoThat's not specific to Objective-C, it's a GCC extension to the C language.
- saagarjha 7y ago(Which Clang happens to support, hence it working here.)
- crusso 7y ago"Think the ternary operator, but worse" Giving the developer succinct ways to prevent program failure from unexpectedly undefined values is a feature.
- ChrisMarshallNY 7y ago"Giving the developer succinct ways to prevent program failure from unexpectedly undefined values is a feature." Most languages have had this since the 1960s. It's called the "if" clause. The ternary and nil-coalescing operators are shorthand (sugar) for an if test (or guard, in Swift). Like I said, I actually like them, but I have to be careful, when using them. I really like Swift, but it's quite possible to write completely inscrutable code in it.
- eugeniub 7y ago> 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.
- ChrisMarshallNY 7y agoYes, 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.
- FpUser 7y ago>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
- 7y ago
- gnud 7y ago... 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.
- eugeniub 7y agoStrongly 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" }
- bdisnainc 7y agoThat 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.
- Razengan 7y ago> 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.
- Razengan 7y agoThink I like others may have read that wrong and it was meant as a joke.