Why are generics called generics?

后端 未结 9 840
野性不改
野性不改 2021-02-04 00:48

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 i

相关标签:
9条回答
  • 2021-02-04 01:28

    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?

    0 讨论(0)
  • 2021-02-04 01:35

    "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.

    0 讨论(0)
  • 2021-02-04 01:35

    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

    0 讨论(0)
  • 2021-02-04 01:35

    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.

    0 讨论(0)
  • 2021-02-04 01:37
    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.

    0 讨论(0)
  • 2021-02-04 01:41

    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.

    0 讨论(0)
提交回复
热议问题