Ternary operator

1,119 views
Skip to first unread message

Rui Ueyama

unread,
Sep 1, 2014, 7:24:36 PM9/1/14
to golang-nuts
My friend asked me a question which I didn't know the answer. A google search didn't seem to give me the answer too, so let me ask here.

What's the rationale behind the decision not to provide the ternary operator? I expect it was discussed and rejected.

I'm not suggesting anything, but just trying to understand why Go is in the current shape.

andrewc...@gmail.com

unread,
Sep 1, 2014, 7:46:15 PM9/1/14
to golan...@googlegroups.com
My guess is that two ways to conditionally assign a value was decided to be a stylistic issue.
Another thing is I noticed the Go spec has all operators as left associative, C like ternary operators are right associative which might be another factor.
It could be as simple as the designers being burnt by bugs caused by ternary operators being abused too often.

andrewc...@gmail.com

unread,
Sep 1, 2014, 7:50:04 PM9/1/14
to golan...@googlegroups.com, andrewc...@gmail.com
https://golang.org/doc/faq#Does_Go_have_a_ternary_form
The tone of this faq makes me think its avoiding redundancy in favour of common style.

Rob Pike

unread,
Sep 1, 2014, 8:00:23 PM9/1/14
to Andrew Chambers, golan...@googlegroups.com
Go has lots of ternary operators. If you're speaking about the lack of
a?b:c, which for some reason is often called "the" ternary operator,
it's because it is the source of too many readability debates and much
ugly code. Better not to have it.

-rob

David Thomas

unread,
Sep 1, 2014, 10:34:13 PM9/1/14
to golan...@googlegroups.com, andrewc...@gmail.com
After reading this, I just have to ask: what ternary operators does Go have? I looked at the spec, and I only see unary and binary operators. So I'm confused, unless you are allowing for built-in functions or user-defined functions to count as operators. Am I missing something?

Skip Tavakkolian

unread,
Sep 1, 2014, 11:55:46 PM9/1/14
to David Thomas, golan...@googlegroups.com, andrewc...@gmail.com
Range?
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rob Pike

unread,
Sep 2, 2014, 1:10:22 AM9/2/14
to Skip Tavakkolian, David Thomas, golan...@googlegroups.com, andrewc...@gmail.com
a, ok := v.(type)
v, present := m[x]
x, ok := <-c

Those are ternary. Well, the map one is quaternary, I guess.

-rob

Thomas Bushnell, BSG

unread,
Sep 2, 2014, 1:04:14 PM9/2/14
to Rob Pike, Skip Tavakkolian, David Thomas, golan...@googlegroups.com, andrewc...@gmail.com
Don't forget slicing:

foo[a:b]




-rob

Rob Pike

unread,
Sep 2, 2014, 1:14:45 PM9/2/14
to Thomas Bushnell, BSG, Skip Tavakkolian, David Thomas, golan...@googlegroups.com, andrewc...@gmail.com
And even foo[a:b:c].

-rob

Thomas Bushnell, BSG

unread,
Sep 2, 2014, 1:21:56 PM9/2/14
to Rob Pike, Skip Tavakkolian, David Thomas, golan...@googlegroups.com, andrewc...@gmail.com
I wish Go supported quaternions, because if it did, I have an awesome pun right now.

Joshua

unread,
Sep 2, 2014, 1:34:11 PM9/2/14
to golan...@googlegroups.com, r...@golang.org, skip.tav...@gmail.com, davidth...@gmail.com, andrewc...@gmail.com
Punny features are very important.
Reply all
Reply to author
Forward
0 new messages