| ▲ | How many options fit into a boolean?(herecomesthemoon.net) |
| 52 points by luu 4 days ago | 30 comments |
| |
|
| ▲ | ralferoo a day ago | parent | next [-] |
| True | False | FileNotFound was a meme about 2 decades ago, and even that was a reference to MSDOS from another 2 decades earlier. I guess things never change, only the language. 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. |
| |
| ▲ | hinkley a day ago | parent | next [-] | | I did a govt contract early on and learned that yes/no/unanswered/unasked was a common quad. I see that in disclosures when applying for jobs as well. | |
| ▲ | gizmo686 a day ago | parent | prev | next [-] | | I'm surprised that trinary logic has not become a standard part of standard libraries yet. Almost every project I have worked on ends up with some form of a yes/no/maybe abstraction. | | |
| ▲ | hinkley a day ago | parent | next [-] | | With privacy coming back into vogue, it’s useful to distinguish “we didn’t ask” from “they wouldn’t answer” For some vector logic the distinction could matter. | |
| ▲ | Aurornis 21 hours ago | parent | prev [-] | | Yes/No/Maybe is a good fit for an enum because “Maybe” carries some specific information. 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. |
| |
| ▲ | nine_k 17 hours ago | parent | prev [-] | | This just means that the problem requires more than a Boolean, but rather something like boolean | error. In many languages from the OOP heyday that alternative part was expressed via throwing an exception. |
|
|
| ▲ | gima 20 hours ago | parent | prev | next [-] |
| "How many options fit into a boolean?"
Five or two, according* to Microsoft. > 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 |
| |
| ▲ | xen0 6 hours ago | parent | next [-] | | That is... amazing. I think my favourite part is the fact that '1' isn't even one of the supported values. | |
| ▲ | rf15 20 hours ago | parent | prev [-] | | Funnily enough that was also my first idea upon reading the headline. 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. | | |
| ▲ | gizmo686 19 hours ago | parent [-] | | I've implemented trits a bunch of times and have never regretted it. How you get 5 possible values into a tri state Boolean is beyond me though. |
|
|
|
| ▲ | gizmo686 a day ago | parent | prev | next [-] |
| It's not clear from the article, but "niche optimization" does not mean "optimization that is only useful in a very specific circumstance". 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. |
|
| ▲ | pavon a day ago | parent | prev | next [-] |
| Neat. Even knowing about niche optimization I would have guessed that you could fit 7 Options - one bit for each. But the developers were smart enough to take advantage of the fact that you can't have a Some nested below a None, so you only need to represent how many Somes there are before you reach None (or the data), allowing 254 possibilities. |
| |
| ▲ | gizmo686 a day ago | parent [-] | | I doubt they were thinking about Option<bool> when making niches work like this. 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. |
|
|
| ▲ | nine_k a day ago | parent | prev | next [-] |
| The scoop: a boolean can't be smaller than a byte. Full 254 level of nested Option<bool> fit into it. (C++ needs much more for even a single level.) |
|
| ▲ | shagie a day ago | parent | prev | next [-] |
| For Java developers... you can use Optional<Boolean> to store the elusive four possible booleans. |
| |
| ▲ | nitnelave 21 hours ago | parent [-] | | 5, no?
Null, Optional::empty, Optional(null), Optional(true), Optional(false) | | |
| ▲ | shagie 21 hours ago | parent [-] | | 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);
| | |
|
|
|
| ▲ | priowise a day ago | parent | prev | next [-] |
| This question always reminds me that we often compress far more nuance into binary decisions than reality allows.
In practice most systems end up inventing “soft booleans” (flags, states, priorities) to deal with that. |
|
| ▲ | johnthescott 11 hours ago | parent | prev | next [-] |
| a two bit, boolish data type used for simple replies from a network: true, false, null and waiting. called a "rummy". |
|
| ▲ | vadelfe a day ago | parent | prev | next [-] |
| The deeper you go into memory layout, the more you realize that even "simple" types aren't that simple. |
|
| ▲ | RobotToaster a day ago | parent | prev | next [-] |
| >and that it takes up one byte of memory You can make them smaller using bitfields in C. |
| |
| ▲ | AlotOfReading a day ago | parent | next [-] | | The object it's inside will still take up at least one byte. sizeof(struct {bool a:1;}) == sizeof(char);
| | |
| ▲ | hinkley a day ago | parent [-] | | Amortization. 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. | | |
| ▲ | hinkley 19 hours ago | parent [-] | | 3^5 is 243 so one could also call an optional Boolean 1/5th of a byte, though 1/4 is so much simpler to read and write. | | |
|
| |
| ▲ | russdill a day ago | parent | prev | next [-] | | Um, no. Please show me how you can fit 255 possible states in something smaller than a byte by using bitfields. | | |
| ▲ | RobotToaster a day ago | parent [-] | | I was quoting the first paragraph, where it says a single normal bool takes a byte. |
| |
| ▲ | 5o1ecist 4 hours ago | parent | prev [-] | | [dead] |
|
|
| ▲ | mock-possum a day ago | parent | prev [-] |
| > looking at Rust … it turns out that `Option<bool>` takes up exactly one byte of memory, the same as bool! The same is true for `Option<Option<bool>>`, all the way up to 254 nested options. Ah how many of those options fit into that boolean. Word games! |