No and there likely never will be:
x := val ? foo() : bar()
Versus var x Type // now this is default initialized, which may or may not be a valid state for your program
if val {
x = foo()
} else {
x = bar()
}
Honestly, I'd be fine with it being for initialization only (like the walrus operator)The go alternatives are too horrible to even contemplate, but here goes:
if a {
f(b, d, e)
} else {
f(c, d, e)
}
or var tmp WhatFuckingTypeIsThisAgain
if a {
tmp = b
} else {
tmp = c
}
f(tmp, d, e)
It's hard to say which one is worse, but there is no doubt that the C version is "unquestionably easier to read."Re; your example, what about
tmp := a ? b : c
f(tmp, d, e)
Even in C++ that's likely how I'd write it.Go core are very smart people so I’m sure they have their reasons, but my Lisp-addled brain can’t understand why you’d ever want statements instead of expressions with potentially discarded values.
I sadly don’t see it getting added to Go.
v := if foo() {
7
} else {
11
}
But this means {} blocks now have values, which is weird in a C derivative. Of course, one could do v := func() int {
if foo() {
return 7
} else {
return 11
}
}()
but... that's kind of awful. x = bar()
if val {
x = foo()
}
Is both shorter and functionally more equivalent with the ternary example.I'd still rather a ternary over any of the above options though!
x := func() Type {
if val {
return foo()
}
return bar()
}()edit: Wrong, see below