5 ms·
There's a niche language called Sail that has & and | act as both bitwise and boolean operators. It actually works really well and isn't confusing at all after
by IshKebab 3y ago
There's a niche language called Sail that has & and | act as both bitwise and boolean operators. It actually works really well and isn't confusing at all after the initial "this is different". There's no real downside because it is a modern strictly typed language unlike C.
On the other hand I don't know if there's a real upside either. It's not very difficult to use && and || and it serves as extra documentation, and everyone is used to it by now.
- secondcoming 3y agoWell in C-derived languages && and || will short-circuit the evaluation whereas & and | will not.
- shagie 3y agoAside, In Oral Tradition in Software Engineering by Bryan Cantrill, he talks a bit about the lack of logical xor in C. https://youtu.be/4PaWFYm0kEw?t=2236&si=MZPbopZPQtnnT-Tb https://youtu.be/4PaWFYm0kEw?t=2236&si=MZPbopZPQtnnT-Tb
- 48864w6ui 3y agoForths often use -1 as TRUE so logical and bitwise operators coincide.
- lifthrasiir 3y agoSo did BASIC traditionally, with no short-circuiting behavior AFAIK. At least Visual Basic has `AndAlso` and `OrElse` variants that do short-circuit though.
- masklinn 3y agoMost langages with a passing link to C support & and | on booleans (even when they have a legitimate non-integer boolean type). However they are eager which makes them generally useless. ^ is a lot more valuable.
- cyco130 3y agoPascal also uses the same operators for bitwise and logical and and or and they’re simply and and or.
- pdw 3y agoI'm not familiar with Sail, but the reason for separate logical operators is that you want short-circuiting behavior. For example, in: (a != NULL) && a->x You don't want to right-hand side to be evaluated if the left-hand side is false.
- Denvercoder9 3y agoI'm also not familiar with Sail, but you could have a single token ("&&") behave as either a bitwise or logical AND, depending on the context: if both operands are an boolean, operate logically, if both operands are an integer, operate bitwise. The complexity lies in what you do with mixed expressions. One solution is to just forbid them. This makes bitwise expressions nested within logical expressions more verbose (you'd need to add a cast), but I'd wager that those aren't used often in most high-level languages.
- IshKebab 3y agoIt does short circuit if the parameters are booleans.