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.
nameLabel.text = user?.name ?? "Guest"
This, as opposed to: if let name = user?.name {
nameLabel.text = name
} else {
nameLabel.text = "Guest"
}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.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.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.Giving the developer succinct ways to prevent program failure from unexpectedly undefined values is a feature.
int a = b ?: c ?: 0I 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
> The name "Elvis operator" refers to the fact that when its common notation, ?:, is viewed sideways, it resembles an emoticon of Elvis Presley.
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.
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.
int a = b ?: c; 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.
a && a.b && a.b.c
(with a short-circuiting &&.) 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 : bBut also, as another commenter pointed out, there is a distinction between "optional chaining" and "nil coalescing" and I explained (roughly) what optional chaining is.
Is that the one you had in mind?
a ?? b
Where this is shorthand for: a != nil ? a! : b
idk what cascaded means, but it sounds like a ternary operation from hell.https://stackoverflow.com/questions/2100758/javascript-or-va...
optional1 ?? optional2 ?? optional3 ?? ifeverythingelsefailsvalueif b is not nil, then a = b else a = c
return a or b