Description
Go version: 1.13
Introduction
There is recurring proposal about "ternary operator" in the issues (e.g. 1, 2, 3...). This suggestion is in hope to reduce their frequency in future.
Problem
Trouble seems to have two parts:
- Go currently intentionally lacks any form of "conditional expression" (either ternary operator or something alike) which could be used inside expression for brevity.
- It is not well explained in FAQ, which leads readers to feel protest and even disgust, and, probably, adds to repulsing newcomers :)
It seems currently there is strong opposition to improve the 1-st part. Then perhaps we can improve the 2-nd?
Further I shall copy current text and proposed change to it, but here are main points:
- motivation uses word UNQUESTIONABLY which is quite questionable approach and have air of insolence, rudeness and doesn't look like professional language
- another ambiguous word is CLEARER (code without conditional expression) which fails to impress after example of 5 additional lines (with 6-th missing) suggested as substitution
- motivation refers to unspecified "language designers" who "had seen (what they didn't like) too often" which sounds like decision was someone's fancy, rather than it is based on popular (though still controversial) opinion in community
- the text doesn't give hope for improvements (alternatives) in future versions
- proposed workaround example is incomplete (lacking variable definition - which looks like omission by intention to reduce line count) and, perhaps it could be shorter
Supposed update to text
this variant is a bit lengthy, feel free to suggest cuts
Currently there is no conditional expression in Go (neither ternary expression, nor anything similar). General workaround is:
var n someType
if expr {
n = trueVal
} else {
n = falseVal
}
which often may be shortened to (at the expense of reassignment):
n := falseVal
if expr { n = trueVal }
The reason for absence of conditional expression in Go is the popular opinion among developers (in various languages), that it may lead to poor code readability and style, e.g.:
- using ternary operator instead of conditional statement (not expression) for brevity;
- using conditional expressions with very long parts split into several lines;
- using nested ternary operator which makes precedence ambiguous.
Future versions of Go may introduce some alternatives (like allowing return value from if-else
or type parameters for functions which will allow creating custom operator-like function etc.). Please don't post yet another issue "add ternary operator". It is omitted by design and is not going to be added at least in the same form as in C++
and Java
.
Current Text
There is no ternary testing operation in Go. You may use the following to achieve the same result:
if expr {
n = trueVal
} else {
n = falseVal
}
The reason ?: is absent from Go is that the language's designers had seen the operation used too often to create impenetrably complex expressions. The if-else form, although longer, is unquestionably clearer. A language needs only one conditional control flow construct.
UPD: yet one more about the same #36303