Synthetic Sugar: Boolean literals
iddan opened this issue ยท 4 comments
Overview
Boolean literals to i32 and Boolean()
(requires Number()
for interop)
Impact
Medium: easier to understand boolean values while maintaining them at compile level
Details
false => i32 0
true => i32 1
false + any TypeError
Number(false) i32 0
Number(true) i32 1
Number(false) + 5 5
false && true false
Boolean(0) false
Due Date
default
Kinda tangential, but in some languages (basically, almost all non-C languages around the time C first arrived) use true === all bits set (in other words, -1 for i32).
The benefit of that is that bitwise and logical operators are the same, or nearly the same in the case of bitwise AND:
!true === ~true
and~false === !false
(true | value) === true
(true & value) === value
Sadly, this breaks with JavaScript convention, which uses C-style 1, that is: (true | 0) === 1
, so true = -1
is more like something the Engineer would have to decide to use when handling true and false.
Yeah, this is pretty necessary to have at this point. Thanks!
I've been almost able to copy paste around JS code into Walt, but I keep running into fixing all the bools. Since I'm feeling the pain I can only imagine someone coming into the project fresh having to deal with this haha.
It'll probably introduce it with tiered releases. As true
and false
are trivial to introduce but the type errors are a bit more work as they require new types. And only compile-time types too.
Boolean()
and Number()
would probably be static type-casts though. As Number
is not a type and Boolean
will likely make more sense as a <number> : bool
as things stand currently. They would also be purely developer convenience additional type safety as the WebAssembly could care less what we type-hint the values, as an i32
is an i32
no matter what at the end of the day. I could maybe see keeping Boolean()
but Number()
is just not specific enough :/