Open TheOnlySilverClaw opened 1 week ago
I want to avoid adding things merely for the convenience of C bindings. Is there some other use for this?
I can't think of much. The name was deliberately chosen to reflect that usage.
I know it sounds a little weird, but so far all the C bindings I've used had at least one place where it would have been useful and I've tried emulating it with enums and bitstructs each time.
It almost nearly works this way:
bitstruct CBool : CInt {
bool flag;
}
fn void test_cbool() @test {
CBool value = { false };
assert((CInt) value == 0);
value = (CBool) 1;
assert((CInt) value == 1);
assert(value.flag == true);
}
It's just slightly short of being convenient with all the casts required.
So maybe a more general case could be an inline
bitstruct?
You could do:
enum CBool : CInt
{
FALSE,
TRUE
}
And now your call becomes:
foo(TRUE);
foo(FALSE);
Yes, been there. I guess it works. Would you mind adding that to the standard library types?
Done!
See if it works as expected.
For type-safe interaction with C APIs, it would be helpful to have a CBool type that behaves like an integer that can only be assigned 0 or 1 and be implicitly cast to bool where needed.
I know integers can already be used in conditions, but this would also communicate the allowed values and prevent accidental assignment of a wrong one for functions like these: