2 ms·
The problem is that I can't declare the variable and set it's value as part of the same operation. I'm forced to declare a variable with a nonsensical or incorr
by vec 9y ago
The problem is that I can't declare the variable and set it's value as part of the same operation. I'm forced to declare a variable with a nonsensical or incorrect value then correct it later as part of a logically distinct statement. This introduces a window into my code where some piece of memory is known to be wrong and puts the onus on me to not use it between its declaration and it's being set.
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.
- randomdata 9y ago> The problem is that I can't declare the variable and set it's value as part of the same operation. 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?
- vec 9y agoOkay, "can't" is too strong a word. But I don't know a good word for "it's technically possible but is cumbersome, is not idiomatic, and requires introducing another stack frame for no good reason".
- randomdata 9y ago> and requires introducing another stack frame for no good reason 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.