Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Unfortunately there's still no guarantee for which variant will be represented by 0


You may also have cases where no variant is 0 so this case is probably not as straightforward


The comment is fair. There have been some talks about being able to allow end users specify niches so that if you could have something like

    enum E<T> {
        Empty = 42,
        Some(NonFortyTwo<T>),
    }
in the same way that the following is of size T today

    enum E<T> {
        Empty = 0,
        Some(NonZero<T>),
    }
but that's not possible today.


Yes, I can see uses for this. Forty-two is obviously not something you'd actually want, but I can see needing numbers in the range 0-100 and being frustrated that since I need 0 I can't carve out a niche even though I don't mind losing 101 through 255 from a u8 instead of making my type wider.

Still the existing tricks get a lot done for relatively little work. I have used Option<NonZeroUsize> to do roughly what I'd use a single integer for in C, but making explicit that zero isn't just zero, but "I dunno, invalid". Would I have done that even if it cost more space? Probably, but it was cool that I didn't even need to consider that, "Zero cost abstraction".




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: