-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
language: bool to numeric and numeric to bool type conversions #7657
Labels
Comments
> Today if you want to convert between a numeric type and the "bool" type you have to create a function to do it. You can convert between any numeric type and bool with the expression "n != 0". (That one fewer character than the proposed "bool(n)".) You say "I often need to convert a slice of numbers to bools or bools to numbers in a loop." Can you please provide a concrete example? |
The code that converts between the slices/arrays of bools and ints is closed source. Sorry about that. I can work something up if you want but the last time I did this for an issue, everyone complained that my simplified example was too simplistic. I know about "n != 0" and I should have addressed that in the proposal. The simple answer is that when you are converting a type to a different type it should be very clear that you are doing that. In my opinion "bool(n)" makes it very clear. Recognizing "n != 0" is idiomatic. The fact the it's one less character is largely irrelevant. |
I assume for m[v] you mean a map to convert from bools to a number? That's clever. I hadn't done that myself because I hoped the compiler would inline the function I made to do it and perhaps get rid of the branches as well. 1. This is going to boil down to how people feel about "idiomatic" vs "standard" type conversions. If you are OK with these particular conversions being idiomatic and don't mind sprinkling map or function declartions throughout your code to convert bools to numbers then this change isn't for you. 2. If on the other hand you've ever wanted to do a bool/numeric conversion and felt that the code should *read like a conversion*, then this change is for you. 3. I want to point out that this isn't really much of a language change, it's more of relaxation of the current compiler implementations. The language already accepts the construct from a syntax/grammar standpoint. The current versions just disallows these particular conversions. As language changes go, this is about as small a language change as is possible. As noted above, the language change is documented by adding "or bool types" to a single sentence of the reference spec. |
Ad "*read like a conversion*". That's IMO a non goal. Math has no (well defined) mapping between booleans and numbers - that's why it's a mess in some languages. Code, on the other side, can produce different numbers conditionally, where the condition is a boolean. IOW, it should look like code, not like a conversion which it isn't. |
Status changed to Duplicate. Merged into issue #6011. |
This issue was closed.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
The text was updated successfully, but these errors were encountered: