可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
"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.
回答2:
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.
回答3:
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.
回答4:
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:
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 >.
回答6:
If they called it "type parameter(s)" people would confuse it with parameters of type Type.
Also, ArrayList isn't "generic". It ONLY works with types of object. If you ask it for something, it will give you an object reference. That's a very specific behavior.
回答7:
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.
回答8:
Because you are creating "Generic" code that will be capable of operating on any type (within constraints you specify) in the same way...
a good example you are familiar with is the Add operator is just about any language... it can "Add" integers, floats, doubles, decimals, binarys, hexadecimals, regardless of whether they are signed, unsogned, how many bits they are, etc...
回答9:
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?