At the risk of becoming the village idiot, can someone explain to me why generics are called generics? I understand their usage and benefits, but if the definition of generic is "general" and generic collections are type safe, then why isn't this a misnomer?
For example, an ArrayList can hold anything that's an object:
ArrayList myObjects = new ArrayList(); myObjects.Add("one"); myObjects.Add(1);
while a generic collection of type string can only hold strings:
var myStrings = new List<string>(); myStrings.Add("one"); myStrings.Add("1");
I'm just not clear on why it's called "generic". If the answer is "...which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code." from here, then I suppose that makes sense. Perhaps I'm having this mental lapse because I only began programming after Java introduced generics, so I don't recall a time before them. But still...
Any help is appreciated.
Definition: “A generic type is a generic class or interface that is parameterized over types.” Essentially, generic types allow you to write a general, generic class (or method) that works with different types, allowing for code re-use.
Generics enable the use of stronger type-checking, the elimination of casts, and the ability to develop generic algorithms. Without generics, many of the features that we use in Java today would not be possible.
When a generic method is called, the compiler determines the actual types to use for the type parameters from the context. Comparable<T> is. is an interface defined in the Java class libraries.
Generic classes encapsulate operations that are not specific to a particular data type. The most common use for generic classes is with collections like linked lists, hash tables, stacks, queues, trees, and so on.
"Generic" is talking about the implementation. You write a single "Generic" list implementation that works with any type, instead of having to write specific implementations for each type you want to use.
I think the right answer to questions like this is almost always "historical reasons, mostly". Generics could just as well have been called "schemes" or "classes" or "type families" or "genera" or "type functions" or "statics" or "Greek types" or any of a million other things. Long ago someone decided to use the word "generic", and it stuck.
"Generic" in the Java sense dates back at least to the mid-1970s. The U.S. Department of Defense was honing a requirements document for its new programming language (what would become ADA). An early draft ("Woodenman", August 1975) says:
Compile time parameters are needed in extensible languages to permit specification of generic procedures and data structures such as stacks, and queues without repeating the definition for each element type.
This is the only use of "generic" in the document. It's not clear to me how it was intended. But by July 1977 ("Tinman") there was a whole paragraph on generics, and the term had clearly come to mean something specific:
12D. GENERIC DEFINITIONS
It shall be possible to define functions, procedures, and types with parameters that are instantiated during translation at each call. Such parameters may be any defined identifier (including those for variables, functions, or types), an expression, or a statement. These parameters, like all other parameters, shall be evaluated in the context of the call.
By June 1978 ("Steelman") it was established jargon; there were other uses of the term "generic" in other sections of the document, clearly referring to this feature. In the finished language, generic
was a reserved word.
The authors of these documents are listed on the site, and presumably most are still around. It would be neat to call them up and ask what they remember.
The earliest plausibly related use of "generic" I found in academia was in Robin Milner's "A theory of type polymorphism in programming" (1978) (and he feels compelled to explain what he means by "generic", so it can't have been in common use in academia at that time):
So this is the generic type of map, that is, to any occurrence of map within the scope of this declaration must be assigned some substitution instance of this type.
"Generic type variable" became CS jargon.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With