How many options fit into a boolean?
Posted by luu 3 days ago
Comments
Comment by ralferoo 9 hours ago
Even now, I still find myself using true/false/null on occasions, but I'm usually smart enough to replace it with an enum at that point. The only time I don't is when it's an optional parameter to a function to override some default/existing value, at which point it then makes sense to keep it as an optional bool.
Comment by hinkley 9 hours ago
Comment by nine_k 4 hours ago
Comment by gizmo686 9 hours ago
Comment by hinkley 9 hours ago
For some vector logic the distinction could matter.
Comment by Aurornis 8 hours ago
For more common situations where the yes/no bool is not available yet or should not be considered, constructs like Rust’s Option<bool> are a very good fit. Layering the bool inside of an Option creates intentional handling about the presence or lack of value first before you can work with it.
Comment by gima 7 hours ago
> MsoTrioState is "a tri-state Boolean value". it has five possible values. only two of them are supported.
*) https://learn.microsoft.com/en-au/dotnet/api/microsoft.offic...
Sourced from here https://fedi.lynnesbian.space/@lynnesbian/115969259564305759
Comment by rf15 7 hours ago
So let's remember: some programmer, somewhere, is right now thinking about building a tri-state boolean because they think it fits their current problem perfectly fine. And they are always wrong.
Comment by gizmo686 7 hours ago
How you get 5 possible values into a tri state Boolean is beyond me though.
Comment by pavon 10 hours ago
Comment by gizmo686 9 hours ago
Option<NonZeroU32> seems like a much more reasonable to justify this with. Also, enums can easily have invalid bit patterns that are unused without there being any specific bit that is always available. All you need is a single variant of the enum to have a free bit, and you have a niche to shove None into.
Comment by gizmo686 9 hours ago
It is a specific optimization based on the idea of storing one type inside of another type by finding a "niche" of unused bit pattern(s) inside the second type.
It has far more useful application than a tower of Option 254 deep.
Comment by nine_k 10 hours ago
Comment by shagie 9 hours ago
Comment by nitnelave 8 hours ago
Comment by shagie 8 hours ago
public static void main (String[] args) {
Optional<Boolean> n = Optional.ofNullable(null);
Optional<Boolean> e = Optional.empty();
System.out.println(n.equals(e));
}
true
https://ideone.com/EGRdi5A null in an Optional is empty. So you've got:
Optional<Boolean> n = null;
Optional<Boolean> e = Optional.empty();
Optional<Boolean> t = Optional.of(Boolean.TRUE);
Optional<Boolean> f = Optional.of(Boolean.FALSE);Comment by priowise 10 hours ago
Comment by vadelfe 9 hours ago
Comment by RobotToaster 9 hours ago
You can make them smaller using bitfields in C.
Comment by AlotOfReading 9 hours ago
sizeof(struct {bool a:1;}) == sizeof(char);Comment by hinkley 9 hours ago
If one Boolean must be a byte then 8 must be eight bytes. Which is not true. A boolean can be 1/8th of a byte which is a meaningful distinction.
Comment by hinkley 6 hours ago
Comment by gottheUIblues 2 hours ago
Comment by russdill 9 hours ago
Comment by RobotToaster 9 hours ago
Comment by mock-possum 10 hours ago
Ah how many of those options fit into that boolean. Word games!