bitwise operators ~ | & |= &=
Closed this issue · 6 comments
When I first designed the language, I realized the need to use &
and |
to represent union and intersection of types. Thus, at the value level, it made sense to interpret them as the same operations on Set
s. Ceylon didn't have bitwise operations at all, because:
- At the time I didn't view it as a language for pushing bits and bytes around.
- We didn't even have a
Byte
type, nor theBinary
abstraction, and, furthermore,Integer
did not specify its representation. - I didn't want to have operator overloading for
&
and|
. - I robbed
^
for exponentiation, and gave it a higher precedence than|
. - Parsing the bitshift operators
>>
and<<
is really much too painful, and I'm always getting confused between>>
and>>>
.
Since then, we introduced a Byte
type, we added well-defined bitwise and bitshifting operations to Integer
, and ultimately I've found myself using Set
s much less than I expected, and Byte
s much more. Oh, and we've already implicitly overloaded some operators to work on Java types.
Therefore, I propose to overload the operators |
, &
, |=
, and &=
to apply not only to Set
s, but also to Binary
s, and introduce a prefix ~
for bitwise complement.
Note:
- I'm not proposing to add
^
(xor),<<
,>>
, or>>>
. - Overloading these operators introduces an ambiguity, for types that inherit
Set & Binary
, but I think that problem is pretty easy to resolve.
I've now got this working for the Java backend, and pushed it to the bitwise
branch.
I still need to implement it for the JS backend.
Alternatively, if you guys really want xor and bitshifting, we could name the bitwise operators (~)
, (|)
, (&)
, (^)
, (>>)
,(<<)
, and (>>>)
. And that would mean no need for overloading. Just let me know.
Isn’t it possible to do the following?
- Do the overloading thingie;
- Deprecate the operators for
Set
; - After a while, remove the operators for
Set
, keeping them only for bitwise operations.
Or do you still think they are useful for sets?
Personally, I don’t mind either way; I have used bytes and needed bitwise operations in the past whereas I have rarely used sets (and never needed to perform intersection/union on them), but I don’t think I’ve needed bitwise operations enough for me to care about operators. It’s not like imma do fancy bitwise shenanigans in Ceylon as one would do in C for example.
So, whether you keep the operators for sets, for bitwise ops, for both, or remove them altogether, I wouldn’t really mind. (Although I don’t really like the idea of operator overloading that much, to be honest.)
Oh and another thing, how would assignment operators look for the parenthesed operators?
While it is rare to use union and intersection on sets, it is by no means so rare that having operator support for these wouldn't be useful.
This functionality is now complete and available on master
.
Unless anyone has any objections, I'm pretty much happy and ready to close this issue.
Done. I'm going to close, but feel free to post feedback here, if any. I can easily reopen the issue.