5 ms·
Void* and java's object both support that idiom, at least to the degree to does. Actually, I'm having trouble thinking of a static language that doesn't support
by jamesaguilar 12y ago
Void* and java's object both support that idiom, at least to the degree to does. Actually, I'm having trouble thinking of a static language that doesn't support it at least as well as go.
- masklinn 12y ago> Void* and java's object both support that idiom I may be mistaken, but IIRC C-style casting from a (void *) provides no correctness guarantees, you may well end up with complete garbage unless you took care to explicitly check your union. And dynamic_cast requires RTTI. So the idiom is supported in the loosest possible sense, there's no language-provided safety. > at least as well as go. Yes, I think that's a point of contention and I edited my comment to expand on it: I really, really don't think Go supports this use case nicely.
- pcwalton 12y ago> I may be mistaken, but IIRC casting from a (void *) provides no correctness guarantees, you may well end up with complete garbage unless you took care to explicitly check your union. Right. In fact, in C (NB: not C++) you don't even have to cast, as the compiler allows it as an (extremely unsafe) automatic conversion :)
- waps 12y agoLanguages that support algebraic data types have a much better solution. You can explicitly declare which types can occur and it can be "A or B". It's called ADTs and it's the real solution to these problems. http://en.wikipedia.org/wiki/Algebraic_data_type http://en.wikipedia.org/wiki/Algebraic_data_type
- masklinn 12y ago> Languages that support algebraic data types have a much better solution. Which I mentioned in my original comment… > You can explicitly declare which types can occur and it can be "A or B". It's called ADTs and it's the real solution to these problems. Well no actually, an ADT is a single type with multiple data-divergent constructors (although GADT may fit the bill, maybe with datakinds, I've yet to understand those). In `data List a = Nil | Cons a (List a)`, the only type is List. Which is why I specifically noted that a closed set was necessary for tagged unions. There are languages which safely[0] handle actual type unions (genuine type coproducts), they are much rarer than types supporting simple tagged unions. IIRC, /u/munificent did a post on that for his magpie language although I don't remember if he ever implemented it. David Pierce's Whiley also seems to use type unions[1] [0] an important discriminant, C has unions but they're not memory — let alone type — safe. [1] http://whiley.org/2013/07/31/understanding-why-union-types-are-useful/ http://whiley.org/2013/07/31/understanding-why-union-types-a...