# Replace ternary \_ ? \_ : \_ operator with a binary \_ ? \_ operator

**URL:** <https://forums.swift.org/t/replace-ternary-operator-with-a-binary-operator/11063>\
**Category:** Pitches\
**Created:** [March 16, 2018, 9:55pm UTC](https://forums.swift.org/t/replace-ternary-operator-with-a-binary-operator/11063 "2018-03-16T21:55:46Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexandre\_Lopoukhine](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexandre_lopoukhine/32/2885_2.png) [@Alexandre\_Lopoukhine](https://forums.swift.org/u/Alexandre_Lopoukhine)\
**Post date:** [March 16, 2018, 9:55pm UTC](https://forums.swift.org/t/replace-ternary-operator-with-a-binary-operator/11063/1 "2018-03-16T21:55:46Z")

</div>

I'm not sure whether this has been proposed, but since there's a switch extension for `_ ? _ : _` floating about, I was thinking that it could be the last time for me to propose to get rid of the ternary operator

My first objection to it is that it is the only `?` in the language not associated with optionals AFAIK.  
My second is that it is the only ternary operator.  
My third is that it is entirely redundant with the following substitution:

```swift
infix operator ? : TernaryPrecedence

private func ? <Value>(lhs: Bool, rhs: @autoclosure () -> Value) -> Value? {
    if lhs {
        return rhs()
    } else {
        return nil
    }
}

```

Here are some example uses:

```swift
let thisAndThat = this ? that ?? false // this ? that : false
let somethingIfThat = that ? something // that ? something : nil

let notThis = maybeThat.flatMap { $0 != this ? $0 }

```

As a side-point, which might not be terribly relevant, I feel that the `??` function with the non-optional rhs should probably be renamed as to be more explicit. Maybe `?:` would be appropriate here.

Compare:

```swift
let hello = this ?? that ?? else
let hello = this ?? that ?: else

```

In a world with the explicit `?:` operator, a human skimming the code can know, without prior information about the types of `this`, `that`, and `else`, that the `hello` is non-optional.

The reason I mention it is that, with this new operator, the diff between the old and the new lines is one character:

```swift
let thisAndThat = this ? that ?: false
let thisAndThat = this ? that : false

```

---

<div class="post-metadata">

**Author:** ![MutatingFunk](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mutatingfunk/32/984_2.png) [@MutatingFunk](https://forums.swift.org/u/MutatingFunk)\
**Post date:** [March 16, 2018, 10:07pm UTC](https://forums.swift.org/t/replace-ternary-operator-with-a-binary-operator/11063/2 "2018-03-16T22:07:31Z")

</div>

I support this idea, but it doesn't require removing the ternary - only introducing a standalone `?` which returns an optional. The ternary operator's implementation shouldn't need changing, even if the mental model changes. As users see it, ``c ? a : b`` would be equivalent to ``c ? a ?? b``, but with different precedence rules.

---

<div class="post-metadata">

**Author:** ![masters3d](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@masters3d](https://forums.swift.org/u/masters3d)\
**Post date:** [March 16, 2018, 10:15pm UTC](https://forums.swift.org/t/replace-ternary-operator-with-a-binary-operator/11063/3 "2018-03-16T22:15:40Z")

</div>

[@forum\_admins](https://forums.swift.org/groups/forum_admins) is there a way to move this [swift-evolution/commonly\_proposed.md at master · apple/swift-evolution · GitHub](https://github.com/apple/swift-evolution/blob/master/commonly_proposed.md) to the forum?

---

<div class="post-metadata">

**Author:** ![anthonylatsis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anthonylatsis/32/39232_2.png) [@anthonylatsis](https://forums.swift.org/u/anthonylatsis)\
**Post date:** [March 16, 2018, 11:24pm UTC](https://forums.swift.org/t/replace-ternary-operator-with-a-binary-operator/11063/4 "2018-03-16T23:24:23Z")

</div>

> [@Alexandre\_Lopoukhine](#):
>
> ```swift
> let thisAndThat = this ? that ?? false // this ? that : false
> 
> ```

How would this help if the return type of your operator is always optional? The ternary conditional operator's return type is optional only if it's generic parameter is inferred as an optional type.

```swift
let foo: Bool? = true ? false ?? true

let foo: Bool?? = true ? false ?? nil

```

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [March 16, 2018, 11:42pm UTC](https://forums.swift.org/t/replace-ternary-operator-with-a-binary-operator/11063/5 "2018-03-16T23:42:02Z")

</div>

Removing the ternary operator from the language is a [commonly rejected idea](https://github.com/apple/swift-evolution/blob/master/commonly_proposed.md). Unless you've got a significant insight not mentioned previously, this is not going to be in scope for discussion.

---

<div class="post-metadata">

**Author:** ![Jon\_Hull](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_hull/32/678_2.png) [@Jon\_Hull](https://forums.swift.org/u/Jon_Hull)\
**Post date:** [March 17, 2018, 6:44am UTC](https://forums.swift.org/t/replace-ternary-operator-with-a-binary-operator/11063/6 "2018-03-17T06:44:42Z")

</div>

Removing the ternary operator is one of the commonly rejected ideas, but I am in favor of leaving that alone while adding an operator which returns an optional value based on a boolean statement. I have this in a scripting DSL of mine and it is really lovely.

The syntax I use is `?(a) b` where the value is b if a is true, and nil if a is false. This combines with my `|` operator which is the same as Swift's `??`.

The reason I find it such a big win is that it lets me compactly chain together a number of `else if` style statements:

```
x = ?(a) b | ?(c) d | e

```

which is equivalent to the following in swift:

```
let x:Type
if a {
    x = b
} else if c {
    x = d
} else {
    x = e
}

```

I'd be strongly in favor of adding something with this functionality if we were to bikeshed the syntax a bit (since ? is too easily confused with ?? IMO), and leave the tertiary alone for now.

---

<div class="post-metadata">

**Author:** ![QuinceyMorris](https://avatars.discourse-cdn.com/v4/letter/q/d26b3c/32.png) [@QuinceyMorris](https://forums.swift.org/u/QuinceyMorris)\
**Post date:** [March 17, 2018, 7:20am UTC](https://forums.swift.org/t/replace-ternary-operator-with-a-binary-operator/11063/7 "2018-03-17T07:20:41Z")

</div>

FWIW, ternary `? :` chains, too:

```swift
let x = a ? b : c ? d : e

```

(and it's a bit more lightweight, since it doesn't construct any optionals that it immediately unwraps).

---

<div class="post-metadata">

**Author:** ![Jon\_Hull](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_hull/32/678_2.png) [@Jon\_Hull](https://forums.swift.org/u/Jon_Hull)\
**Post date:** [March 17, 2018, 7:36am UTC](https://forums.swift.org/t/replace-ternary-operator-with-a-binary-operator/11063/8 "2018-03-17T07:36:15Z")

</div>

Hmm... good point. You could also always just do:

```
let x = a ? b : nil

```

for the nil behavior. My DSL had some other ways which this syntax stacked nicely with other parts of the language too, making it a huge win overall, but I don't think most of those are relevant to swift.

I guess I am back on the side of just leaving things as they are then...

---

<div class="post-metadata">

**Author:** ![Alexandre\_Lopoukhine](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexandre_lopoukhine/32/2885_2.png) [@Alexandre\_Lopoukhine](https://forums.swift.org/u/Alexandre_Lopoukhine)\
**Post date:** [March 17, 2018, 10:35am UTC](https://forums.swift.org/t/replace-ternary-operator-with-a-binary-operator/11063/9 "2018-03-17T10:35:50Z")

</div>

So, my proposal contains three core ideas:

1. Remove `_ ? _ : _`
2. Add `_ ? _`
3. Replace `??` with `?:` in the non-optional rhs case

Since I'm well acquainted with the commonly rejected proposals, 1 was a bit cheeky. Let's forget about that for the time being, as I still think 2 and 3 are worth discussing.

The idea of an explicit `?:` is one that I've thought about for a while, I'm pretty confident that it enhances "scanability", and is generally a good move.

I've introduced the ? operator into my own apps/frameworks/experiments, just to see what would happen, and I quite like the general feeling of using it.

---

<div class="post-metadata">

**Author:** ![Alexandre\_Lopoukhine](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexandre_lopoukhine/32/2885_2.png) [@Alexandre\_Lopoukhine](https://forums.swift.org/u/Alexandre_Lopoukhine)\
**Post date:** [March 17, 2018, 11:06am UTC](https://forums.swift.org/t/replace-ternary-operator-with-a-binary-operator/11063/10 "2018-03-17T11:06:57Z")

</div>

The line you quoted was written with only the addition of the binary `?` operator in mind.

If we don't split the operators, your code becomes the following:

```swift
let foo = true ? false ?? true // Inferred Bool
let foo = true ? false ?? nil // Inferred Bool?

```

If we split the overloaded `??` operator into `??` and `?:`, then that becomes:

```swift
let foo0 = true ? false ?? true // Inferred Bool?
let foo1 = true ? false ?? nil // Inferred Bool?
let foo2 = true ? false ?: true // Inferred Bool
let foo3 = true ? false ?: nil // Inferred Bool?

```

The last case may be surprising, but that's because promotion to optional is weird. That, by the way is also a potential problem with the example you gave, since the number of `?`s after `Bool` is fairly arbitrary. The type of the expression on the right hand side only gives a lower bound on the depth of nesting of optionals, `let foo: Bool?????? = ...` would also have compiled.

---

<div class="post-metadata">

**Author:** ![anthonylatsis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anthonylatsis/32/39232_2.png) [@anthonylatsis](https://forums.swift.org/u/anthonylatsis)\
**Post date:** [March 17, 2018, 11:30am UTC](https://forums.swift.org/t/replace-ternary-operator-with-a-binary-operator/11063/11 "2018-03-17T11:30:51Z")

</div>

If here

```swift
let foo = true ? false ?? true

```

`?` is your custom operator defined in your root post and `??` is the nil-coalescing operator, then `foo` will be `Bool?` Note you return `Value?`.

Excuse me if I am misunderstanding something.

---

<div class="post-metadata">

**Author:** ![Alexandre\_Lopoukhine](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexandre_lopoukhine/32/2885_2.png) [@Alexandre\_Lopoukhine](https://forums.swift.org/u/Alexandre_Lopoukhine)\
**Post date:** [March 17, 2018, 11:37am UTC](https://forums.swift.org/t/replace-ternary-operator-with-a-binary-operator/11063/12 "2018-03-17T11:37:07Z")

</div>

This is exactly the confusion that I'm talking about. If you actually do this in a playground, then the inferred type is `Bool`. This is because the `??` operator uses the non-Optional rhs variant, effectively of type `(Bool?, Bool) -> Bool`.

If we un-overload the `??` function, then the inferred type becomes `Bool?`. We would have to use the explicit non-Optional operator `?:` to get the inferred type to be `Bool`.

---

<div class="post-metadata">

**Author:** ![Tino](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tino/32/1073_2.png) [@Tino](https://forums.swift.org/u/Tino)\
**Post date:** [March 17, 2018, 11:55am UTC](https://forums.swift.org/t/replace-ternary-operator-with-a-binary-operator/11063/13 "2018-03-17T11:55:35Z")

</div>

Swift bureaucracy is a tough business, but I hope you'll have the stamina to push this into review ;-)  
Afaics, the other plans to deprecate the ternary suggested to replace it by changing `if` or `switch`, so your pitch is different.  
Looking at [[swift-evolution] ternary operator ?: suggestion](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151214/002609.html), there is a list of positive aspects of the ternary:

- It is extremely concise, and covers a very common pattern.√
- It is pervasively standardized in a very wide range of languages.
- It’s weird syntax reduces the odds that people would flow it out and use very large expressions in it.√
- It chains well for multiple conditions because of its associativity.√

Imho your operator keeps all important ones, and it removes a special case from the language. Apparently, it's much easier to get some new special cases into the language as long as they add a tiny bit of convenience, and removing a feature will always cause some churn.  
But without cleanup, we'll pile up a lot of legacy, which makes the language complicated and ugly.

---

<div class="post-metadata">

**Author:** ![anthonylatsis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anthonylatsis/32/39232_2.png) [@anthonylatsis](https://forums.swift.org/u/anthonylatsis)\
**Post date:** [March 17, 2018, 12:00pm UTC](https://forums.swift.org/t/replace-ternary-operator-with-a-binary-operator/11063/14 "2018-03-17T12:00:50Z")

</div>

No photoshop, I promise

 ![57%20PM](https://global.discourse-cdn.com/swift/original/2X/2/210341026e50989b1cd0f078e5d11bb4ff6969da.png)

The inferred type indeed is `Bool`, but the return type is `Bool?`

---

<div class="post-metadata">

**Author:** ![Alexandre\_Lopoukhine](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexandre_lopoukhine/32/2885_2.png) [@Alexandre\_Lopoukhine](https://forums.swift.org/u/Alexandre_Lopoukhine)\
**Post date:** [March 17, 2018, 12:06pm UTC](https://forums.swift.org/t/replace-ternary-operator-with-a-binary-operator/11063/15 "2018-03-17T12:06:20Z")

</div>

That's because the NilCoalescing precedence is higher than Ternary.

Also no photoshop:

```swift
precedencegroup BinaryPrecedence {
    associativity: right
    higherThan: NilCoalescingPrecedence
}

infix operator -? : BinaryPrecedence

let foo = true -? false ?? true
type(of: foo) // Bool.Type

```

---

<div class="post-metadata">

**Author:** ![anthonylatsis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anthonylatsis/32/39232_2.png) [@anthonylatsis](https://forums.swift.org/u/anthonylatsis)\
**Post date:** [March 17, 2018, 12:35pm UTC](https://forums.swift.org/t/replace-ternary-operator-with-a-binary-operator/11063/16 "2018-03-17T12:35:12Z")

</div>

Ah, you want

```swift
let foo = (true ? false) ?? true

```

Yes, that will work

---

<div class="post-metadata">

**Author:** ![Nicole\_Jacque](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nicole_jacque/32/245_2.png) [@Nicole\_Jacque](https://forums.swift.org/u/Nicole_Jacque)\
**Post date:** [March 18, 2018, 1:40am UTC](https://forums.swift.org/t/replace-ternary-operator-with-a-binary-operator/11063/17 "2018-03-18T01:40:10Z")

</div>

@masters3d I've created a post with a link to it and pinned it to the top of Pitches.
