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

2.0: make Bool !<: Integer? #50128

Open
jariji opened this issue Jun 11, 2023 · 6 comments
Open

2.0: make Bool !<: Integer? #50128

jariji opened this issue Jun 11, 2023 · 6 comments
Labels
kind:breaking This change will break code kind:speculative Whether the change will be implemented is speculative
Milestone

Comments

@jariji
Copy link
Contributor

jariji commented Jun 11, 2023

I find (1, 0) == (true, false) causes confusion. I think it would be clearer and less bug-prone without this feature.

It would be good to list use cases for the current behavior and find good replacements for them.

  • sum(bools) could be count(bools)
  • mean(bools) could be mean(float, bools)
@jakobnissen
Copy link
Contributor

What kind of confusion does it lead to?

@jariji
Copy link
Contributor Author

jariji commented Jun 11, 2023

For example, in indexing, the basic property that x == y implies f(x) == f(y) fails.

julia> let ix1 = [true, true]
           ix2 = [1, 1]
           v = [10, 20]
           ix1 == ix2, v[ix1] == v[ix2]
           end
                   
(true, false)

This example is also due to the unfortunate type-based overloading of getindex.


It also behaves differently from all the other built-in integers - it widens rather than wrapping:

julia> typemax(Int) + 1
-9223372036854775808

julia> typemax(Bool) + 1
2
julia> true + true
2

@jariji
Copy link
Contributor Author

jariji commented Jun 11, 2023

Fixing getindex is a better solution. Its behavior shouldn't depend on the type of the index argument.

@jariji jariji closed this as completed Jun 11, 2023
@StefanKarpinski StefanKarpinski added the kind:speculative Whether the change will be implemented is speculative label Jul 18, 2023
@StefanKarpinski StefanKarpinski changed the title 2.0: Disconnect false/true from 0/1 2.0: make Bool !<: Integer? Jul 18, 2023
@StefanKarpinski
Copy link
Sponsor Member

StefanKarpinski commented Jul 18, 2023

I've reopened this because I do think it's worth considering for 2.0 whether Bool should be a subtype of Integer or not. As long as Bool is a number then I think we need to have true == 1 and false == 0 but if we moved Bool out of the numeric hierarchy entirely, then it could be ok to not have those equalities. Then we'd probably need to introduce a UInt1 type that basically behaves like Bool currently does, however, at which point you have to wonder a little if we should have bothered. But like I said, worth keeping an issue open to remind us to consider it. Whether indexing should treat booleans specially is another question; it's certainly a bit strange for Bool to be a kind of number but have indexing with a boolean vector do something completely different. If booleans weren't integers then it would be more justifiable, but I also think that removing boolean indexing might be a good idea too.

@nsajko
Copy link
Contributor

nsajko commented Jan 12, 2024

@jariji
Copy link
Contributor Author

jariji commented Feb 7, 2024

I just meant to write rand() < .36 to get 36% trues and instead wrote rand(Bool) < .36 which gives 50% trues. A MethodError would've caught that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:breaking This change will break code kind:speculative Whether the change will be implemented is speculative
Projects
None yet
Development

No branches or pull requests

6 participants