It's so irritating that something as simple as logical and/or isn't the same in different languages :/
In some languages 0 or 5 is 1, in others 0 or 5 is 5
It's so irritating that something as simple as logical and/or isn't the same in different languages :/
In some languages 0 or 5 is 1, in others 0 or 5 is 5
Suppose I could make an inline function like
int or(int x, int y) {
if (x) return x;
return y;
}
ok i think this will work even if nested, without statement expressions (so will compile on MSVC)
ORV(x, y) (tmp = (x) ? tmp : (y))
ANDV(x, y) (tmp = (x) ? (y) : tmp)
the AND version felt dicey but i realized that if you get to the (y) expression the tmp value is never evaluated so it's fine if it gets clobbered
@eniko If you do you own function, why don't use if/then/else instead of 'risking' with the ? thing ? .. At this point I won't change too much I suppose.
@eniko so this is neat, but I'm mostly curious if you're able to provide a bit more insight into what you're using this for
I've gotten super Rust-brained lately so I'm having trouble understanding the benefits of mixing up integers and booleans like this
@clarfonthey i'm developing an IR for a virtual machine and when i was making and/or yesterday i was just treating zero as false and anything else as true
so `0 or 5` is 5, because 5 is true
then i realized that's inconvenient if you wanna do like `(a && b) == (c or d)` because im not coercing truthy values to 1 so this wouldn't work
i also kinda wanna make this transpilable to human-readable C and realized C's logical operators always coerce the result to 0 or 1 (likely for exactly this reason)
then i realized other languages like lua and javascript actually do it kinda the way i was doing it and i realized i kinda wanna support being able to do it both ways
but if i want value selecting logical operators, and C only has boolean logical operators, and i wanna transpile to C, then i have to find a way to make those work in C
@gilesgoat because you can't do if/then/else inline in an expression