Why are generics called generics?

2020-05-22 07:49发布

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.

9条回答
做自己的国王
2楼-- · 2020-05-22 08:07
bool Equals(int x, int y)

The above method can only compare integers, so we can say that it is specialized for comparing integers.

bool Equals<T>(T x, T y);

The above method can compare values of any type, so we can say that it isn't specialized for any particular type - it's generic.

查看更多
狗以群分
3楼-- · 2020-05-22 08:09

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.

查看更多
再贱就再见
4楼-- · 2020-05-22 08:10

I dont want to get into the semantics of language (english, not java), and at the risk of answering you with a tautology; a generic method is called generic because, as you said, its can be used the the general sense, it doesnt have a specific type, it can be used generally

查看更多
迷人小祖宗
5楼-- · 2020-05-22 08:10

A class which takes objects is NOT generic, it is very specifically taking a type which is itself a generic type. A generic class, on the other hand, can be used with any specific type.

查看更多
我只想做你的唯一
6楼-- · 2020-05-22 08:13

Okay, take this with a grain of salt, because I'm totally guessing, but I wonder whether it might be a bastardization of "Generative Types".

Conceptually, when you specialize a List into a List< String >, it generates a new type. At least, that's the way it works in C++ templates and in C# generics.

In Java, since the parameterizations are discarded by the compiler using type erasure, it actually doesn't generate a new specialized type, so who knows?

I suppose you could say that Java implements a genericized version of generative types :)


ON EDIT:

Here's another point of view...

The type List< String > is not what they're talking about when they refer to a "generic" type. I think the terminology is actually referring to the List< T > type, which is how the type exists in its generic form. List< String > is a specialization of the generic List< T >.

查看更多
聊天终结者
7楼-- · 2020-05-22 08:15

I am not a "native" English Speaker, so I could be wrong but the point of the "Generics" is that the Define Generics types, isn't it?

查看更多
登录 后发表回答