Hacker News new | ask | show | jobs
by m0th87 4258 days ago
> Your examples aren't quite equivalent -- `people` in Go refers to a contiguous chunk in memory of size len(people) x sizeof(People).

How it's laid out in memory is tangential to the original critique - namely, that Go introduces complexity by not having parametric polymorphism.

> But we are doing something you can't do at all in Python (barring specialized array modules or similar).

Python not only allows this (I don't see why it wouldn't), it has built-in support for an equivalent for sorting via rich comparison methods [1]. The point is, though, that you don't have to.

As for the Go example, it's not equivalent because you're writing your own boilerplate code. And it's throwing static typing out the window, which doesn't seem very idiomatic.

1: https://wiki.python.org/moin/HowTo/Sorting#Odd_and_Ends

1 comments

Hmm? Sorry, I wasn't trying to say Python doesn't let you define comparison functions, I was just reiterating that it doesn't let you sort an array of embedded objects, as opposed to pointers.

This is not a tangential difference. It's a real, important language decision, with benefits and consequences.

If you restrict yourself to arrays of interface{}, like python, then you can do exactly what python does, at the cost of losing compile-time type checking...exactly like python :)

I believe my example is equivalent to the python code; it doesn't contain any boilerplate (which is something you have to write over and over again), but rather a short library function, SortBy, which you would only have to write once.

(The normal Go sorting routines do contain boilerplate, since the Swap and Len methods are basically copy-paste jobs.)

That said, I agree using interface{} everywhere is not idiomatic Go, and certainly using things like map and fold in Go would be so awkward as not to not be worth it.

But doing something like writing a heap class that required casting when removing the object would be pretty reasonable, IMO.

You're not throwing static typing out the window, you're just losing it in that isolated case. It's more like...pushing static typing slightly out of the comfy spot on the couch. That's unfortunate!...but maybe not that bad -- after all, dynamic languages get along alright w/o static checks anywhere. :)