-
Notifications
You must be signed in to change notification settings - Fork 18.3k
Closed
Labels
FrozenDueToAgeLanguageChangeSuggested changes to the Go languageSuggested changes to the Go language
Description
Overview This proposal would allow bidirectional type conversions between all of the builtin scalar numeric types and the builtin "bool" type. The syntax and grammar of the language already allows these conversions but the current compiler doesn't implement these particular conversions and gives error messages. As has been standard since "C" was developed the numeric value of 0 is considered "false" and any non-zero numeric value is considered "true", but the canonical value of "true" is numeric 1. Description Today if you want to convert between a numeric type and the "bool" type you have to create a function to do it. For example: http://play.golang.org/p/jUlDoCCI5N var itob = func(int i) bool { if i == 0 { return false } return true } var btoi = func(bool b) int { if b { return 1 } return 0 } These functions also define the semantics I propose for the conversions proposed. Because there are currently no generic functions and no trinary operator one often has to implement multiple versions of the above functions. If this proposal is accepted and implemented the following two conversions and others like them would no longer generate the following compiler errors in the cg compilers: var i int= 1 b := bool(i) i := int(b) ./bool.go:24: cannot convert i (type int) to type bool ./bool.go:25: cannot convert b (type bool) to type int Justification After several years of Go programming I long for this functionality. I often need to convert a slice of numbers to bools or bools to numbers in a loop. Having to call a user defined function for each conversion is slow and in many (all) cases these conversions can be handled without branches and in any case these conversions can and should be inlined. Implementation The implementation requires that the compilers be updated to allow these conversions and generate code for each case. The scalar numeric types affected are: uint8, uint16, uint32, uint64, int8, int16, int32, int64, float32, float64, uint, int, byte, rune Since I have seen integers passed as a uintptr using OpenGL via Cgo I suggest that the type "uintptr" also be allowed as a numeric type that can be converted to and from a "bool". Documentation In the "Conversions" section of the "The Go Programming Language Specification" the following line would change from: "x's type and T are both integer or floating point types."" to: "x's type and T are both integer, floating point, or bool types." Issues I am unsure of how to handle floating point +0.0 and -0.0. Right now I think only regular 0.0 is "false" but I am unsure about this. Related Issues https://golang.org/issue/6011 This is similar to #6011 except the proposed syntax is different. I believe the syntax proposed here is more consistent with the spirit of the language and is simpler too. Importantly, I would like to point out that the Type Assertion syntax isn't needed here because any numeric value can be converted to a "bool" and any "bool" can be converted to a numeric value so these conversions can never fail.
Metadata
Metadata
Assignees
Labels
FrozenDueToAgeLanguageChangeSuggested changes to the Go languageSuggested changes to the Go language