Skip to content
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

proposal: Go 2: Allow to convert bool to int #63544

Closed
vituchon opened this issue Oct 14, 2023 · 8 comments
Closed

proposal: Go 2: Allow to convert bool to int #63544

vituchon opened this issue Oct 14, 2023 · 8 comments
Labels
LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Milestone

Comments

@vituchon
Copy link

vituchon commented Oct 14, 2023

Proposal: Allow to convert bool expression to int, assuming that false is 0 and true is 1.

Motivation: I think is a common scenario where you have to write count += cond ? 1 : 0 so allowing to write an expression like int(<bool expr>) that yields 1 or 0 will allow, in turn, to write count += int(<bool expr>) . I Mean. i'm pretty sure that anyone stomped into a situation where you have to count elements of a collection that fulfill a condition.... and in the concrete case of golang I guess a canonical way to model it is using the "for range" statement.. specially if that collection is a slice.

Aditional Context: I ignore the complexity of this, is just a wish and perhaps a nice to have. I read the conversion section of the language spec and nothing is stated about this convertion.

Final words: Keep up the good work.

Greetings

Víctor

@vituchon vituchon added LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change labels Oct 14, 2023
@gopherbot gopherbot added this to the Proposal milestone Oct 14, 2023
@seankhliao
Copy link
Member

Duplicate of #45320

@seankhliao seankhliao marked this as a duplicate of #45320 Oct 14, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Oct 14, 2023
@vituchon
Copy link
Author

vituchon commented Oct 14, 2023

If it is not to much to ask, ¿can you tell me something regarding this? I saw that this is indeed something proposed by others before me... ¿it is really complicated?

@ianlancetaylor
Copy link
Contributor

Every language change has costs and benefits. The question is not whether the change is complicated. The question is whether the benefits of the change are worth the costs. This was discussed in #45320 and the decision was that it wasn't worth doing. We aren't going to revisit that decision unless there is new information to consider.

@vituchon
Copy link
Author

vituchon commented Oct 15, 2023

Thanks @ianlancetaylor... but there are a lot of comments in previous chats and perhaps we could simplified this... let's try make a summary... shall we?

Benefits

  • Economy of expression: Allow to write count += int(<bool expr>) without resorting to using functions or map or write 4 lines of code... all in one line.

Cost

* To be fill...

TD;DR;

Regarding performance:

I intentionally leave out of the table performance affairs as they are not a primary concern regarding software development unless explicitly stated in the proposal spec. Perhaps doing this on a language could carry some heavy burden, I don't know... you may tell me. I read some comments about performance and I do not get them.

@ianlancetaylor
Copy link
Contributor

Respectfully, we have to be able to make decisions and move on. Otherwise we will wind up spending all of our time rehashing old decisions and will be unable to make forward progress. As you say, there are a lot of comments in #45320. We considered them, we decided, and we moved on. We aren't going to return to the issue unless there is some new information that was not previously considered. Thanks for your consideration.

@chmike
Copy link

chmike commented Oct 21, 2023

The main (and only?) reason in favor for a bool to int conversion is optimization as it allows to get rid of conditional instructions which breaks pipelining. It is currently impossible in Go.

I however don't think the proposal is good in that it would punch a hole in the typing compartmentalization which is a safeguard against errors.

I would consider the addition of an explicit type conversion function like math.Float64Bits a better approach. It would not alter current language specification, and it would make the conversion fully explicit and readable. I expect that its use would then also be limited to where it really makes sense.

@randall77
Copy link
Contributor

The main (and only?) reason in favor for a bool to int conversion is optimization as it allows to get rid of conditional instructions which breaks pipelining. It is currently impossible in Go.

This is easily done in Go today. For instance, this function compiles down to a single zero-extension instruction, no conditionals anywhere:

func f(b bool) int {
	r := 0
	if b {
		r = 1
	}
	return r
}

@vituchon
Copy link
Author

vituchon commented Oct 21, 2023

Hi @chmike and @randall77 regarding this proposal I moved the discussion into this golang nuts thread. In my last email there is a recap (with links and references) about this affair.

To summarize

  • There isn't stated any technical reason, so it looks like it is a matter of personal taste.
  • I found two groups of people: Those who like it and those who doesn't like it.
    • According to this issue initial comment it appears that more people like it that those who doesn't.
      • Based on that fact I timidly proposed that we can perform a vote, if the time and the circumstances allow it.
      • image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Projects
None yet
Development

No branches or pull requests

6 participants