3 ms·
W/o generics you lose two things: 1. Type safety 2. Convenience (1) is certainly no worse than a dynamic language, where you don't have type-safety anywhere.
by dilap 13y ago
W/o generics you lose two things:
1. Type safety
2. Convenience
(1) is certainly no worse than a dynamic language, where you don't have type-safety anywhere. (2) doesn't seem that bad to me. You'll write code like this:
x := customStack.Pop().(SaysHello) // error here if x isn't a member of the interface SaysHello
x.Hello()
As opposed to a dynamic language:
x = customStack.Pop()
x.Hello() // error here if x doesn't define hello
Disclaimer: I've just poked at Go a little bit. I'm thinking about trying it out as a replacement for some Python code I have.
Further disclaimer: As a C++ programmer back in the day I used to make a lot of fun of Java for not having generics (it's had them for a long time now, of course). Lately I've been doing Objective-C (where the situation is like Go, but worse -- no generics nor interfaces; it's all just dynamic), and I've been surprised at how not having generics is Not As Bad As You Might Think. It's basically equivalent to a 'dynamic' language.
- dkuntz2 13y agoYou only lose type safety if you're writing generic containers. If you don't need a customStack or something similar you'll be fine. While I think it's good to know how to write containers, and how they work, how frequently do you really need to write a custom container? What's wrong with the built in ones (provided they exist)?
- dilap 13y agoYes, didn't mean to imply you lose all static type safety -- you're still way better off on that front than in a language like Python or JavaScript. Basically, if you're happy with a dynamic language, Go makes things better most of the time, but sometimes you have fall back to dynamic checking, because of the lack of generics. Custom containers: Well, it's definitely true that you can get pretty far with just arrays and hash tables -- which Go special-cases! -- but you're probably gunna want something fancier eventually...