Terminology - Type Parameter vs Type Variable

26 views
Skip to first unread message

zhong...@gmail.com

unread,
Jul 5, 2015, 2:38:12 PM7/5/15
to java-la...@googlegroups.com
(Warning, too long, uninteresting, do not read :)

In Java generics, we have terms "type parameter" and "type variable". It's a little confusing when to use which. It is not a serious problem, of course; even if misused, the reader will likely understand what we are talking about. Nevertheless, for the interest of pedantry, let me try to discuss it.

First, forget about generics, let's think about "parameter" and "variable" in typical procedure languages. Say we have a method declaration

    void foo(String s)
    {
        int i = 2;
    }

Viewed from outside, we say the method has one "parameter". To invoke this method, an "argument" must be supplied to each method parameter. A parameter has a position, a type, and a name; however, the name is often unimportant; it's the position that matters. The parameter list is part of the method signature.

Viewed from inside, i.e. in the method body, "s" is a variable. It is very much like a local variable, and for most purposes we can think of "s" as a local variable. The name of the variable is important.

However, it's not rare either to refer to "s" as a "parameter" in the the method body. It doesn't sound weird; after all, variable "s" is introduced by the parameter "s". (Sometimes people even say "argument s" in the method body, which probably refers to the initial value of variable "s".)

Let's see the wording of JLS. JLS#4.12.3 - "For every parameter ... a new parameter variable is created each time that method is invoked". Based on this, we could say the official term for variable "s" is "parameter variable".

JLS treats "local variable" and "parameter variable" separately; in places where they are equivalent, both will be enumerated side by side. However it's easy to find texts like "local variables and method parameters" mentioned in the same breath; apparently, "parameter" here is short for "parameter variable".

Therefore, the term "parameter" could carry two meanings; it can be used outside and inside. This is not really confusing because the context makes it clear what the term means.

----

Back to generics. Say we have a generic declaration

    <T extends Number> void foo()
    {
        class S{}
    }

Viewed from outside, we say the generic method has one "type-parameter". To "instantiate" this generic method, a "type-argument" must be supplied for each type-parameter. A type-parameter has a position, a bound, and a name; however, the name is often unimportant; it's the position that matters.

Viewed from inside, "T" is called a "type-variable". It represents a type. If not for erasure we could print its runtime value (i.e. the actual type-argument). A type-variable has a bound an a name; the name is important.

In the declaration body, for most purposes, "T" is just a type. However it's not a constant type like "List<String>"; it is a "varying" type; the instantiated type is the type-argument supplied to the type-parameter.

A type-variable cannot be reassigned; it is "variable" in the sense of a "final variable" being variable.

From inside, we could also refer to "T" as type-parameter; it won't cause any confusions. However, such  usage seems quite rare. Possible explanation - we cannot declare a local type-variable; a type-variable is always from a type-parameter. This is in contrast to "variable" - there are different kinds of variables, therefore it may be clearer to refer to a variable as the "parameter" to stress where it's from.

(A type-variable could also be from wildcard capture, but that is a different story)

Conclusion? When we discuss a generic declaration, as an outsider, we say it has type-parameters of such and such. Within the body of the declaration, use the phrase "type-variable T". Or, simply "type T", if the fact doesn't matter that T is not constant.

Zhong Yu
bayou.io
Reply all
Reply to author
Forward
0 new messages