News for hackers. Stuff that matters.
Re:From Node.js to Go by chimeracoder on Thursday January 29, 2015 @05:12PM (#8969213)
> saying that your type system doesn't have generics is like saying your car doesn't have wheels.

To overburden an analogy, it'd be more like complaining that your tank doesn't have wheels[0], or your hovercraft. Go takes a different approach to the same problem (in this analogy, getting from point A to point B).

But really, this is a rather tired flamewar that gets beaten to death literally every time a post about Go makes the front page, and there's really nothing more that can be said about it. Either program in idiomatic Go, which means using the language as it's designed (ie, without generics, at least in its current form), or don't, but it's very tiresome to see this argument rehashed again and again.

[0] https://en.wikipedia.org/wiki/Continuous_track#mediaviewer/F...

Re:From Node.js to Go by wwweston on Thursday January 29, 2015 @05:28PM (#8969305)
> To overburden an analogy

Sorry. In the spirit of Go, you're not allowed to re-use this analogy; you're going to need to write another different one that can perform a similar function.

It's ok if you copy and paste some of the text from the first analogy and structure it similarly, though.

Re:From Node.js to Go by Touche on Thursday January 29, 2015 @09:36PM (#8970177)
> Go takes a different approach to the same problem (in this analogy, getting from point A to point B).

Go doesn't take any approach to writing generic code at all.

Re:From Node.js to Go by MichaelGG on Friday January 30, 2015 @03:22AM (#8971038)
That's the really disappointing part. I've read the thread where the designers talk about implementing generics, and they basically say that every language's implementation of generics sucks, and since they can't come up with a design that's perfect (no codegen duplication, but still all the perf of specialization), they're just gonna punt.

And then the code I've seen is littered with "interface {}".

That's a pretty big thing to give up, and I don't get the point. Something like F# gives you high level features (and green threads if you want) with fair perf, and Rust gives you a fair amount of language with C perf.

Re:From Node.js to Go by Sphax on Friday January 30, 2015 @03:41AM (#8971081)
I think littered might be a bit strong. I'm writing a service in Go, and have yet to use interface{}. However, you might have to use it more when writing libraries.
Re:From Node.js to Go by burntsushi on Friday January 30, 2015 @08:03AM (#8971770)
That's false. Go has structural subtyping. It enables you to sort collections of things without actually writing a sorting algorithm. Seems like generic code to me.
Re:From Node.js to Go by chaotic-good on Friday January 30, 2015 @08:40AM (#8971913)
It has interfaces to write generic code. It doesn't allow you to write your own generic data structures but it's not an issue because it has built-in dynamic arrays and dictionaries. You supposed to use built in data structures most of the time like with python or ruby.
Re:From Node.js to Go by khyryk on Thursday January 29, 2015 @05:26PM (#8969296)
This happens because almost every time there's someone who more or less asks what in the world generics are for since they can do everything they want to with Go's type system. Sure, it's possible, but it's missing the point.
Re:From Node.js to Go by mitchty on Thursday January 29, 2015 @06:10PM (#8969491)
I always saw it as the classical: why do you want a screwdriver to put the screw in place? The hammer should be good enough!

Well, sure, I guess you can pound a screw into things with a hammer but you're missing the point.

Re:From Node.js to Go by pekk on Thursday January 29, 2015 @06:50PM (#8969655)
Or just build your furniture without using screws, like in Japanese carpentry. When people are already having a decent time building things using the tool, it looks pretty stupid to tell them they've done it all wrong for years in all the decent products they made.
Re:From Node.js to Go by ufo on Thursday January 29, 2015 @08:01PM (#8969891)
Parametric polymorphism gives safety guarantees that you can't easily obtain without language support. For example if you have a function typed "forall a . a -> a" you know it has to be the identity function (or something that bypasses the typesystem, like infinite loops or a runtime exception).
Re:From Node.js to Go by 4ydx on Thursday January 29, 2015 @11:38PM (#8970485)
And in Go since there is no parametric polymorphism you would end up writing either:

1) Methods with names that indicate the parameter types. 2) Method calls on objects to convert some internal struct data from one type to another. 3) Something I haven't thought of that doesn't involve losing a handle on your type.

So there is certainly a loss in convenience, but if you use the type system it can be just as safe as a functional language as far as I can tell. So, yeah, it isn't as intellectually elegant, but it also isn't really that hard to deal with.

Re:From Node.js to Go by ufo on Friday January 30, 2015 @08:09AM (#8971785)
No you can't. The point of generics is that you don't need to specify the parameter types - the parameter type is also a parameter. This means that you can write type safe container operations like "map" and "filter".

The only way to write this kind of generic code in Go is to cast everything to `interface{}`, which can't be checked statically.

If you are still not convinced, one of the things you can do with generics is say "these two parameters have the same type". In Go the best you can do is say "these two parameters are subclasses of something", which is less precise.

Re:From Node.js to Go by 4ydx on Monday February 02, 2015 @12:57AM (#8982902)
Yeah I follow you. I am providing ways of trying to work around the lack of a higher level type class system. As a baseline, I would recommend not jumping outside golang's given type system just because it feels inconvenient for the programmer. That means you don't get to write a generic method for, for instance, all integers. Too bad :P This means the programmer does have to do more work, but the type system is always strictly enforced. That is better in my opinion.
Re:From Node.js to Go by aangjie on Friday January 30, 2015 @01:34AM (#8970737)
Technically a coin should be enough to put a screw in place. winks depending on the size of the screw of course. :-)
Re:From Node.js to Go by Gurkenmaster on Friday January 30, 2015 @05:53AM (#8971419)
Don't worry! All the screws have the same size and your coin is compatible. (Go's builtin data types which have generics)

You want to use different screws? You're on your own.

Re:From Node.js to Go by dragonwriter on Thursday January 29, 2015 @05:42PM (#8969375)
> To overburden an analogy, it'd be more like complaining that your tank doesn't have wheels

Which would be just as bad as for a car, since the things that drive and guide a tank tread are wheels (drive wheels and road wheels.)