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

Casting to bool #2841

Closed
daurnimator opened this issue Jul 7, 2019 · 6 comments
Closed

Casting to bool #2841

daurnimator opened this issue Jul 7, 2019 · 6 comments

Comments

@daurnimator
Copy link
Contributor

I would expect bool(0) to be false and bool(1) to be true.

test "aaa" {
    @compileLog(@typeOf(bool(0)));
}
:29: error: expected type 'bool', found 'comptime_int'
    @compileLog(@typeOf(bool(0)));
@andrewrk
Copy link
Member

andrewrk commented Jul 7, 2019

For bool to int use @boolToInt. For int to bool, use x != 0.

It's intentional that implicit casting an integer to boolean does not work, otherwise this would work:

test "aaa" {
    if (0) {
        // ...
    }
}

@andrewrk andrewrk closed this as completed Jul 7, 2019
@tuket
Copy link

tuket commented Aug 29, 2024

Hey, what happended to @boolToInt?
Looks like it doesn't exist anymore, what should I use instead?

@Rexicon226
Copy link
Contributor

Hey, what happended to @boolToInt? Looks like it doesn't exist anymore, what should I use instaead?

It has been renamed to @intFromBool.

@tuket
Copy link

tuket commented Aug 29, 2024

Hey, what happended to @boolToInt? Looks like it doesn't exist anymore, what should I use instead?

It has been renamed to @intFromBool.

Nope, now you have to use @bitCast

const number : u1 = @bitCast(boolean);

@mlugg
Copy link
Member

mlugg commented Aug 29, 2024

@tuket that is not true -- the comment you replied to is easily verified to be accurate. Please check before commenting incorrect information.

@tuket
Copy link

tuket commented Aug 29, 2024

@mlugg You are right, sorry. I read somewhere else that you can use @bitcast (and it seems to work), but using @intFromBool seems more appropiate.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants