3 ms·
But you can't write generic functions in Go! Oh, wait. He just did. As a caveat to my exasperated sarcasm, I do realize he's using reflection to identify and
by falcolas 12y ago
But you can't write generic functions in Go!
Oh, wait. He just did.
As a caveat to my exasperated sarcasm, I do realize he's using reflection to identify and type the data at runtime, as opposed to compile time as with C++ templating, but this is kind of generalization is still quite useful when writing general purpose library code.
Personally, I'd not be inclined to use this either, the number of times I've actually had to write generic code using reflect in my time writing Go could be counted with one finger.
I do appreciate that it's there, however, since it is what allows the JSON library to do its magic.
- kd0amg 12y agoYeah, my immediate reaction was, "all that work manually doing what a type checker should have done for you?" Given that, it seems sensible enough to just inline and type-specialize the 5 or so lines you actually care about.
- hyperpape 12y agoThis only handles functions of type a -> a -> a (https://github.com/robpike/filter/blob/master/reduce.go https://github.com/robpike/filter/blob/master/reduce.go), whereas a generic reduce takes functions of type a -> a -> b. So this is certainly not proof that you can write generics in go. See also pmahoney's comment in this thread: https://news.ycombinator.com/item?id=9315721 https://news.ycombinator.com/item?id=9315721.
- epidemian 12y agoMinor nitpick: a generic reduce should take functions of type a -> b -> a, where a is the type of the reduced values, and b is the type of the elements in the sequence to be reduced.
- hyperpape 12y agoAh, of course.
- amscanne 12y agoYou could very easily relax that constraint by changing a couple of lines. Rob Pike has simply chosen the classic MapReduce form. I'm not sure why you would need "proof" that generics are possible in Go. You have reflection and type assertions, and their capabilities are well documented. Does it give you generics? All depends on your definition of generics.
- masklinn 12y agoAnd even with that, it still returns an interface{} which you need to cast.
- falcolas 12y ago> This only handles functions of type a -> a -> a So, since his toy code was explicitly written to accept the same type for both parameters of the function, you are unable to see how it could be modified to accept a different type signature for the reduce function? It looks to me like a fairly trivial change to get the type of argument `zero`, and use that as one of the parameters to the reduce function. As for pmahoney's comment, looks like there's a bug. Perhaps he should file a bug report, or pull request.
- TheCoelacanth 12y agoIt's 30 lines long (the equivalent in Haskell would be 1 fairly simple line of code). Type errors are detected at runtime instead of at compile type, and it's not as generic as actual reduce.
- istvan__ 12y agoIn fairness not only in Haskell. I think Go is just not a great choice for this sort of effort. On Haskell though, languages are not a single dimension thing, so it is very easy to say that X is better in Y and leave out all the other dimensions, but that just oversimplification of the problem.
- falcolas 12y ago> It's 30 lines long (the equivalent in Haskell would be 1 fairly simple line of code). Bully for Haskell. Language N can always do it better/faster/shorter than language M. What matters in this case is that it can be done, in a type safe way. > Type errors are detected at runtime instead of at compile type Already mentioned that. > it's not as generic as actual reduce. It's toy code, forgive him for not writing it perfectly. If it can accept one ambiguous type throughout, there's no reason it could not accept multiple types throughout.
- TheCoelacanth 12y ago> If it can accept one ambiguous type throughout, there's no reason it could not accept multiple types throughout. I'm sure it could, but only at the expense of ballooning to an even more disproportionate length.
- falcolas 12y ago> I'm sure it could, but only at the expense of ballooning to an even more disproportionate length. If you looked at the code, you would see that it could be done in zero additional lines of code if he wanted, or in one if he wanted to be explicit. if !goodFunc(fn, elemType, zero.Type().Elem(), elemType) { str := elemType.String() panic("apply: function must be of type func(" + str + ", " + zero.Type().Elem().String() + ") " + str) } There would be a few other inline changes to add `zero` to the function calls (and fix the 0/1 cases), but they are all pretty trivial. If we want to be pedantic about it, the goodFunc call is not even technically required, it just makes the error output a bit better.
- tome 12y ago> But you can't write generic functions in Go! Did anyone claim this? What I heard was that you can't write generic data structures (and I guess that really means type-checked, parameterized data structures). I don't know though, I've never used Go.