-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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: remove @boolToInt
in favor of @bitCast
#15794
Comments
I wish it would expand the size if you add two together when the destination type is a larger size. This code panics: pub export var counter: usize = 0;
pub export var dynamic_bool: bool = true;
test {
counter += @boolToInt(dynamic_bool) + @boolToInt(dynamic_bool);
}
Similarly, I wish it was |
That's essentially widening arithmetic, which is a completely separate proposal, unrelated to
This would either a) break the very basic rule that |
The status quo is that adding two runtime-known I'm not super opinionated about the solution, only that this is a small thing that makes it harder to write reliable software in Zig. |
I'm working on #15701 so that BTW, to "simplify" the language means also we should clean up existing code dependency to To be clear, I'm +1 for this proposal as it works like a charm - I like it. |
I'm not in favor of this, |
However, remember that #5909 is also accepted, which means that
|
Hello, I can't find the function |
It was renamed to |
@Vexu thank you |
As @ifreund pointed out in #7950,
@bitCast
can serve the function of@boolToInt
just fine: #7950 (comment):The purpose of
@boolToInt
is to serve as a branchless alternative toif (x) 1 else 0
because in that case you have to depend on the optimizer to not emit a branch.Advantages
@bitCast(u1, x)
already worksu1
, whereas with@boolToInt
currently you can getu1
orcomptime_int
@bitCast
for doing other things the smart way, too@bitCast
can do easily.Disadvantages
@boolToInt
might make it harder to remember that you should use@bitCast
/@boolToInt
instead ofif (x) 1 else 0
. But one could argue if you don't keep all builtins in your head this doesn't matter anyway.If we wanted to remind people we could even add a little note to the docs of
@bitCast
in the langref but ultimately this is just one of many things that are expected to work with@bitCast
so I wouldn't even say this is noteworthy. So, this point can basically be disregarded.If, like the other
@*to*
builtins,@boolToInt
provided additional safety I would understand its existence but as it stands it doesn't really provide anything that@bitCast
doesn't.The text was updated successfully, but these errors were encountered: