News for hackers. Stuff that matters.
Re:Go by Example by sergiotapia on Friday January 17, 2014 @09:40AM (#7075952)
That's something for the Go creators to decide, but personally I enjoy the feeling of safety that comes with lack of generics. I have very few Go bugs at runtime and I think that explicitness plays a huge part in it.
Re:Go by Example by hdevalence on Friday January 17, 2014 @09:52AM (#7076009)
What? I'm talking about stuff like this, copied from the documentation:

    func (a ByAge) Len() int           { return len(a) }
    func (a ByAge) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
    func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
How does forcing me to type out (or copy-paste) an implementation of Swap increase safety? If anything, it just makes the code more error-prone.

And not having generics means that (as far as I can tell) people end up using a lot of `interface{}`. How does that increase safety?

More importantly, why would I want to use a language whose solution to the problem of "dealing with generic data" essentially boils down to "use a void*"?

Re:Go by Example by JeanPierre on Friday January 17, 2014 @09:47AM (#7075987)
Could you elaborate on how generics makes code less safe? In my experience, the lack of generics is much more type unsafe, as you often have to do casts at runtime (and handle them) when you have to implement your own data structures. In contrast, languages with generics handle those at compile time.
Re:Go by Example by pcwalton on Friday January 17, 2014 @01:26PM (#7077481)
Not having generics reduces explicitness (because you don't specify the types of your containers) and reduces safety (because of all the casts and manual implementations of things like swap functions).
Re:Go by Example by fauigerzigerk on Friday January 17, 2014 @02:40PM (#7077955)
On the other hand, C++ templates sometimes make it hard to understand which function ends up getting called, what size a particular type actually has or what is an alias for what.

There are too many indirections to follow manually. All I know is that the compiler will select some function that has conforming types. The compiler is very very smart. It knows all the incredibly complicated name resolution rules and it will use each one of them.

Unfortuntately I'm not as smart. Even after more than 20 years of using C++ I sometimes fail to anticipate which function the compiler decides to use. And that is not safe.

In fact it is less safe than casting interface{}, because that will at least fail fast at runtime instead of silently doing something unexpected.

Re:Go by Example by scott_s on Friday January 17, 2014 @05:23PM (#7078866)
It is possible to have generics without having the specialization rules of C++.
Re:Go by Example by pcwalton on Friday January 17, 2014 @08:31PM (#7079631)
You're criticizing ADL, not generics.
Re:Go by Example by fauigerzigerk on Saturday January 18, 2014 @03:14AM (#7080511)
It's true that I'm not criticising generics in general, but ADL is only part of what makes C++ templates so treacherous.

I think Go needs some form of generics. Every statically typed language does. But after C++ I do understand the hesitation on the part of Go's creators.