On Wed, Dec 17, 2014, 20:33 null <adon...@google.com> wrote:
I propose the following modest language change for Go 1.5. (This is issue #9367.)
An explicit conversion int(b) where b has a boolean type would become legal, and would yield 1 if b is true, 0 otherwise. Similar conversions would be legal for all numeric types.
Rationale: currently this function cannot be computed in a single expression, despite it being often useful, hard to misuse, efficiently implemented by modern ALUs, present in nearly all other languages, and syntactically natural in Go.
While I appreciate the value of avoiding implicit int/bool conversions, forbidding explicit ones seems slightly obtuse, and as a result of its omission, one must write four extra lines of code:
IMHO this clearly belongs to compiler optimization, not into the language.
-j
IMHO this clearly belongs to compiler optimization, not into the language.
I've wanted this a few times. SGTM.
You're all arguing that a rarely used trivial operation that can be implemented in a couple of simple lines of code needs to be part of the language.
You're all arguing that a rarely used trivial operation that can be implemented in a couple of simple lines of code needs to be part of the language. My response is to write a function for this sort of thing. The two or three times I've needed it, that's what I've done and it did not seem an onerous task.
for range x {…}
This situation seemed awkward, so as of Go 1.4 the variable-free form is now legal. The pattern arises rarely but the code can be cleaner when it does.
for _ = range x {…}
While you're correct, Rob, that casts from bool to int can be implemented in a couple of simple lines of code and that it is not an onerous task to write those, my argument—and I believe the OP's as well—is that this feature improves the consistency of the language.
Traditionally in Unix/Linux, 0 is true and all other values are false. i.e. return codes from programs:
- https://en.wikipedia.org/wiki/True_and_false_%28commands%29
- http://tldp.org/LDP/abs/html/exit-status.html
So if this were to be adopted, true should convert to 0; false should convert to 1.
So if this were to be adopted, true should convert to 0; false should convert to 1
The language is basically done and frozen. These kinds of tiny changes seem reasonable one by one, but if you let them all in you end up with a giant mess like C++. It is especially important to reject the changes that have trivial alternatives, like writing a 4-line function, because those changes are the least necessary.
Traditionally in Unix/Linux, 0 is true and all other values are false. i.e. return codes from programs:
- https://en.wikipedia.org/wiki/True_and_false_%28commands%29
- http://tldp.org/LDP/abs/html/exit-status.html
So if this were to be adopted, true should convert to 0; false should convert to 1.
i'd say no, based on them just being different things, although historically common its not ideal to represent logic with numbers
also because of this the conversion will be a matter of convention not definition, the 'extra' code makes it clear which way is being used.
The discussion about "for range x" is in http://golang.org/issue/6102
and https://codereview.appspot.com/104680043 . You can see that we
didn't rush into it; it was almost a year from proposal to
implementation. An argument in favor of the change was that we can
normally omit the trailing unnecessary variables in language defined
assignment statements: we don't require "for a, _ = range x".
Similarly, we don't require "switch _ = x.(type)".
--
On Thursday, December 18, 2014 5:41:32 PM UTC-7, simon place wrote:i'd say no, based on them just being different things, although historically common its not ideal to represent logic with numbersIt's a valid objection that logic values and numbers are different. However, mapping the two does have a lot of historical precedent (George Boole, as mentioned earlier, plus the Iverson bracket appearing in a programming language in the early 1960s) so it's not that outrageous to provide such a conversion.
also because of this the conversion will be a matter of convention not definition, the 'extra' code makes it clear which way is being used.So why not replace int(myFloat) with a function call that lets you specify the rounding mode? It's a matter of convention, not definition, that int(myFloat) rounds towards zero, and an explicit function call would make it clear which rounding mode is being used.
All that said, the evidence presented is much weaker than you made it out to be.
While you're correct, Rob, that casts from bool to int can be implemented in a couple of simple lines of code and that it is not an onerous task to write those, my argument—and I believe the OP's as well—is that this feature improves the consistency of the language. Go already allows conversions between any of the floating-point and any of the integer types, and it's not like those conversions behave more obviously than {false→0, true→1}. Think of choice of rounding direction or handling overflow cases; those represent more far-reaching judgment calls than how to map Booleans to integers.
isn't that stuff defined in an IEEE standard that Go says it adheres to, precisely so it is defined.
- The Go specification could just as easily have stated it adheres to a definition that mathematicians proposed 160 years ago
- Just like there's not a single definition for bool-to-int conversions, there's not a single definition of float64-to-int conversions. The IEEE floating-point standard in fact defines five rounding modes. Go could have chosen any of those five and still been compliant with IEEE 754. It went with "round towards zero", which is the prevalent rounding mode in today's programming languages, just like {false→0, true→1} is prevalent in today's programming languages.
— Scott