by vec · 8y ago · 0 comments
It's a small nitpick in the grand scheme of things, but it forces me to allow a class of user error (and, in some cases, concurrency issues) that other styles of programming make impossible.
And also it usually adds to the line count, which is annoying.
Care to elaborate? Consider the following:
var foo = func() struct{ Bar int } {
if os.Getenv("BAR") == "1" {
return struct{ Bar int }{1}
}
return struct{ Bar int }{2}
}()
Is there a case where the foo variable can be assigned to something other than one of the two conditional-dependent values?With a given implementation or is there something in the Go specification that prevents this from being compiled away? If I were to write something similar in, say, C++ with a good compiler I would expect the generated code to be equivalent to using a ternary operator.