4 ms·
Yeah, you cant do that in Go, even explicitly: // cannot convert i (variable of type int) to type bool bool(i) youd need to use a function: func
by svnpenn 4y ago
Yeah, you cant do that in Go, even explicitly:
// cannot convert i (variable of type int) to type bool
bool(i)
youd need to use a function:
func to_bool(i int) bool { return i != 0 }
- samatman 4y agoSeems a bit mean-spirited to provide casts but not that one.
- svnpenn 4y agonot really. Go has no concept of truthy: https://developer.mozilla.org/docs/Glossary/Truthy https://developer.mozilla.org/docs/Glossary/Truthy and I fully support that decision. If you want to use a boolean, you need to be explicit about it.
- samatman 4y agoA statically typed language shouldn't indulge in truthiness, no. But a cast is isn't truthiness, it's truth: the meaning of "make this int impenetrable to all but truth and falsehood" is in practice well defined, there's nothing implicit happening here. The compiler isn't going to make that function, it's going to optimize back to a cast to boolean. Why make the poor shmoe user type it out?
- svnpenn 4y agoCast int8 to int16 isn't the same thing as int to boolean. In the first case, you get the same number (ignoring wrapping issues). In the second case, you're asking the compiler to make an arbitrary decision about what numbers go to what booleans.
- samatman 4y agoThere is nothing arbitrary whatsoever about the conversion of any string of bits to a boolean, it is false iff none of the bits are 1. If you want to call it arbitrary, it's been arbitrated decades ago, but Boolean logic is much older than computers and works as it does for a reason. I suspect you know that.
- svnpenn 4y agothe number 9, is not the same thing as "true". some people may want it to be, it might be convenient for some people if a compiler treats them as the same thing, but they aren't. In my opinion, its an anti-pattern to have the compiler make these type of decisions. Instead of writing and reading code that is explicit, you're writing an abbreviated version of what you want, and assuming that the compiler will know what you mean.
- samatman 4y agoIt's Go pretending it's a kind of language it isn't, and making the user do the teapot dance instead of cooperating. People keep telling me this koolaid is delicious but I just don't see it.
- svnpenn 4y agoIt seems you ran out of actual arguments, sorry to hear that.
- samatman 4y agoYou can't argue with someone who doesn't understand logic, which is literally true here. I have run out of jokes at Go's expense.
- svnpenn 4y agoI think I made my point known. Converting int to boolean is not a settled task, as different people might not agree on what the correct way is to do that. False could be zero, or negative one, or any negative number, or even one. So its better not to have the compiler make that decision, and just have the code be explicit about what its trying to do.