# Ternary operator if only syntax

**URL:** <https://discourse.julialang.org/t/ternary-operator-if-only-syntax/51443>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [December 8, 2020, 6:03am UTC](https://discourse.julialang.org/t/ternary-operator-if-only-syntax/51443 "2020-12-08T06:03:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Azzaare](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azzaare/32/21008_2.png) [@Azzaare](https://discourse.julialang.org/u/Azzaare)\
**Post date:** [December 8, 2020, 6:03am UTC](https://discourse.julialang.org/t/ternary-operator-if-only-syntax/51443/1 "2020-12-08T06:03:32Z")

</div>

Hi there!

Is there any good reason to not have an `if` only syntax available for ternary operator where the two following syntax would be equivalent: `a ? b` and `a ? b : nothing` ?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 8, 2020, 6:06am UTC](https://discourse.julialang.org/t/ternary-operator-if-only-syntax/51443/2 "2020-12-08T06:06:40Z")

</div>

Off the top of my head, one problem is then `a ? 1:3` would be really weird (is `1:3` a range or a terinary?)

---

<div class="post-metadata">

**Author:** ![Azzaare](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azzaare/32/21008_2.png) [@Azzaare](https://discourse.julialang.org/u/Azzaare)\
**Post date:** [December 8, 2020, 6:11am UTC](https://discourse.julialang.org/t/ternary-operator-if-only-syntax/51443/3 "2020-12-08T06:11:59Z")

</div>

Space is enforced for ternary operator, so it would be a range ^^

EDIT: to the best of my limited knowledge, I don’t know of a case where we can use `:` with space beside for ternary operator, though my question

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [December 8, 2020, 6:19am UTC](https://discourse.julialang.org/t/ternary-operator-if-only-syntax/51443/4 "2020-12-08T06:19:11Z")

</div>

```julia
julia> 1 : 3
1:3

```

---

<div class="post-metadata">

**Author:** ![Azzaare](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azzaare/32/21008_2.png) [@Azzaare](https://discourse.julialang.org/u/Azzaare)\
**Post date:** [December 8, 2020, 6:22am UTC](https://discourse.julialang.org/t/ternary-operator-if-only-syntax/51443/5 "2020-12-08T06:22:55Z")

</div>

OK, my bad!

Well, thank you and sorry for the time!

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [December 8, 2020, 6:25am UTC](https://discourse.julialang.org/t/ternary-operator-if-only-syntax/51443/6 "2020-12-08T06:25:23Z")

</div>

I don’t see why `nothing` would be a useful thing to return.

If you are looking for something like a “one-line `if`”, there is

```julia
a = 4
a > 3 && println("Big")

```

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [December 8, 2020, 10:16am UTC](https://discourse.julialang.org/t/ternary-operator-if-only-syntax/51443/7 "2020-12-08T10:16:38Z")

</div>

The `nothing` matches what you get out of an `if` expression that doesn’t have an `else` branch.

I really do like the terseness of `&&` and `||`, but I always felt using them this way was a bit of a hack and I used to find it quite unintuitive to read or I would get the logic backwards, and I imagine I can’t be the only one.

As expressions they don’t behave quite like `if` expressions because the untaken branch returns a `Bool` instead of `nothing`. So I always felt there was scope for adding some additional syntax here. (But - more syntax makes for a more complex language, so I’m not sure where the balance lies on this one).

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 8, 2020, 3:26pm UTC](https://discourse.julialang.org/t/ternary-operator-if-only-syntax/51443/8 "2020-12-08T15:26:43Z")

</div>

> [@andyferris](#):
>
> I really do like the terseness of `&&` and `||` , but I always felt using them this way was a bit of a hack

I think that at this point, they are pretty established and widely used in Julia packages.
