From 7b338bcfeb1c476106331a94d32293e8d18de451 Mon Sep 17 00:00:00 2001 From: Acedia / Melancholy <85808999+TheGreatSageEqualToHeaven@users.noreply.github.com> Date: Wed, 22 Jun 2022 15:01:25 -0700 Subject: [PATCH] Create syntax-bitwise-operators.md --- rfcs/syntax-bitwise-operators.md | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 rfcs/syntax-bitwise-operators.md diff --git a/rfcs/syntax-bitwise-operators.md b/rfcs/syntax-bitwise-operators.md new file mode 100644 index 00000000..20178702 --- /dev/null +++ b/rfcs/syntax-bitwise-operators.md @@ -0,0 +1,34 @@ +# Support bitwise operators + +## Summary + +Add bitwise operators and metamethods for bitwise operators + +## Motivation + +Luau currently implements the `bit32` library which adds support for 32 bit numbers. Supporting bitwise operators would add support for all numbers providing a plethora of benefits. + +`bit32.x()` syntax can be complex to write and confuse users with many bit operations. Implementing bitwise operators would also improve performance compared to calling `bit32` functions and metamethod overhead would be less than doing `bit32.x()`, + +Implementing bitwise operators simplifies the learning complexity of learning the bitwise library due to the syntax being universal to most languages. + +## Design + +```lua +x & y __band +x | y __bor +x ^^ y or x ~ y __bxor +~x __bnot +x << y __blshift +x >> y __brshift +``` + +Bxor support is odd because Lua 5.3 added the operation as `x ~ y` and a [Lua patch by Thierry Grellier and Joshua Simmons](https://lua-users.org/wiki/LuaPowerPatches) added it as `x ^^ y` + +## Drawbacks + +Adding bitwise operators would add several new operations to the interpreter and would require new metamethods to be added. Increased compiler complexity. + +## Alternatives + +Implementing a `bit` library that would support all numbers