3 ms·
Ask HN: Have Generics been accepted as “considered useful” in modern languages?
Now that golang 2 will have generics I wanted to see what HN though about generics being generally accepted as useful.
Typescript also supports generics similar to the way Java added them (with type erasure) and I find them amazingly useful vs normal Javascript.
Other language features like checked exceptions are far more controversial.
I think it would be interesting if we've evolved to a point where most programmers felt like generics were a good addition to their languages and that their use was no longer controversial.
The only major downside I see to them is that they add more verbosity.
Also, the type erasure is a major pain and can cause all sorts of confusion but this is mostly an implementation detail.
- RNeff 8y agoGoing back to the dark ages of assembly language, they implemented text macros to ease the programming burden. This worked at a text character level, with passed parameter strings merged into defined text strings. This worked well most of the time. Debugging them was a challenge. Text macros were part of C and C++ and other languages. You could write very clever things, like faking isomorphism in pure C. Algorithm textbooks, like Knuth's books, usually use pseudo-code for their examples, and can be considered 'generics'. I don't think Knuth's books are controversial.There are a number of attempts to re implement textbook algorithms into standard macro or generic packages. Generics are an attempt to add semantics and checking to the macro text substitution process. Expanding the generic body in the context of the full compiler finds lots of errors. Most of my generic work was with Ada(83). The main problem we found was there needed to be a way to define constraints on the type(s) that were passed in as parameters. Types may require an equality, less than, increment, or null for the algorithm to work correctly. Another problem with both macro and generic approaches is the expansion will create a lot of code, not all of which will be used. A generic might create 50 to 100 methods, but only a handful will be used by the program. We talked about some way to specify which methods the program would use to restrict the code generated. (Obviously this feature would require computing the transitive closure of internal method calls) The end goal is that all of the algorithms taught in CS classes would be directly available in my favorite programming language and environment. This does not seem to be controversial.
- WorldMaker 8y agoSo far as I am aware the only language where I've read Generics have ever "considered harmful" is C++'s Templates, which are not exactly Generics as most other languages use them. Generics in Java, Typescript, C# are closer in spirit to functional programming's Algebraic Data Types applied to OOP, whereas C++ Templates are much more of a macro language than that. (C++ templates have been provably Turing complete as an embedded language.) Most of the C++ "considered harmful" reasons for eschewing the Template mini-language don't apply to Generics in the other languages. (It also seems like modern C++ developers have more come to terms with Templates, and a lot of the recent features in that language expect/are built upon Templates.)
- nf05papsjfVbc 8y agoTo be honest, I don't know where this comes from. It is of course possible to do what many would call "overly clever" things with templates in C++. However, in modern C++, even the unappealing syntax is mostly hidden and it is such a pleasure to use the standard library or to build something like it. C++ strongly subscribes to the principle of "if you don't need/use it, you should not pay for it" (in terms of performance). Given that and the constraint of backward compatibility, it does a pretty solid job of providing a kind of generics for a systems language. If those constraints do not apply to someone, they may perhaps be able to find something else which suits their needs better. I do concede, however, that it is a real struggle to learn enough C++ to get comfortable with it and not feel like there are too many booby-traps for one to feel safe with it. So, whenever people ask me if they should learn C++, I discourage them unless they are going to need it for their job, or they intend to go into game development, finance or some such thing. Anyway, I think I've begun to ramble. If you'd like to try and see the thought process behind how C++ came to be what it is, "The Design and Evolution of C++" is an _excellent_ read and I couldn't recommend it highly enough.
- SamReidHughes 8y agoYeah we're way way past that point now.
- jryan49 8y agoMaybe should of made this a poll