4 ms·
> tl;dr This form of genericity is provided by nearly any OO or imperative language. And in fact it points out a shortcoming in Go: it doesn't support parametri
by masklinn 13y ago
> tl;dr This form of genericity is provided by nearly any OO or imperative language. And in fact it points out a shortcoming in Go: it doesn't support parametric polymorphism.
OTOH the feature demonstrated here is discriminated unions, he's actually storing different types in the two nodes and is correct that most statically-typed languages don't have good ways to nicely handle that case (at least when the typeset is open, many languages handle the closed case nicely[0]).
Not that Go handles it nicely of course, as you would in Java you have to use a cast/type assertion at every fetch, and that operation may fail at runtime. So you end up with
val = heap.Pop(key).(*MyType)
which may fault at runtime much like Java's
val = (MyType) heap.pop(key)
or
val, ok = heap.Pop(key).(*MyType)
if !ok {
// handle failure
}
versus
try {
val = (MyType) heap.pop(key)
} except (ClassCastException e) {
// handle failure
}
[0] http://en.wikipedia.org/wiki/Tagged_union http://en.wikipedia.org/wiki/Tagged_union
- jamesaguilar 13y agoVoid* 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 13y 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 13y 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 13y 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 13y 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...
- danieldk 13y agoOTOH the feature demonstrated here is discriminated unions, He is storing interface{}, which is a fat pointer (a tuple of the pointer and type pointer). I don't see what is revolutionary and how most statically typed languages don't handle that case. Sure, in Java the type pointer is stored in the actual instance and not in a fat pointer, but it doesn't make a difference in data structure design. You can do the same abstraction in Java, C++, and C#. Which covers probably 90% of the statically-typed languages out there. Edit: note on the last Java example: an if statement with instanceof look prettier ;).
- masklinn 13y ago> Edit: note on the last Java example: an if statement with instanceof look prettier ;). Sure, and an if or switch on .(type) in go probably looks better as well.
- hornetblack 13y ago> switch on .(type) For anyone wondering switch val := heap.Pop(key).(type) { case (*MyType): // We're all good default: // Didn't get a (*MyType) }