3 ms·
How much are zero values actually zero values (as in, bit patterns of all zeroes), and how much are they instead default values, which could conceivably be comp
by chrismorgan 2y ago
How much are zero values actually zero values (as in, bit patterns of all zeroes), and how much are they instead default values, which could conceivably be computed per type?
Suppose we talk about tagged unions specifically, would it be conceivable to just nominate one variant as the default variant?
Hence, Option[T]’s zero value could be None, and Result[T, E]’s zero value could be Ok(T’s zero value).
(I have no idea if this would work or not. I haven’t used Go for a dozen or so years, but once knew it pretty decently. I work extensively in Rust. But I am curious about this, because I want every language to have sum types, they’re just such a great thing. Always miss ’em when working in JavaScript or most other languages I ever touch.)
- jim-jim-jim 2y ago> Suppose we talk about tagged unions specifically, would it be conceivable to just nominate one variant as the default variant? My experience with Go is just as recent as yours, and I'm happy to eat shit if I'm completely off base here, but my interpretation is that a tagged union with two instances like Some a | None would have to be implemented with underlying standalone `Some` and `None` types, and that `Some` itself would still require an empty representation, which is strange. Compared to Haskell, where for Maybe a = Just a | Nothing the two instances are only (de)constructors, and there is no actual type `Just`.
- lmm 2y ago> would have to be implemented with underlying standalone `Some` and `None` types, and that `Some` itself would still require an empty representation, which is strange. It's not that strange. That's the normal way to model these things in OO languages, and while it's obviously not ideal, it's not completely awful either.
- treyd 2y agoThis works for those two types, but consider something slightly more general like an Either type with a Left and Right. You could just repeat the same think as Ok but for Left or Right, but then why pick one or the other? It becomes a footgun with more complex scenarios to always demand some default value. Also how it interacts with generics needs to be considered.
- chrismorgan 2y agoLook, Result defaulting to Ok is stupid too—it doesn’t have a sensible default value. But Go already has a problem with some of its zero values being stupid. I don’t think proper enums would make it any worse?
- stouset 2y agoI think this is a reasonable take. It might be a stupid decision, but it's at least consistently stupid.