Java Reference
In-Depth Information
of the API require, and only later realize that a type safe implementation was pre-
cluded.
The call from
reverse()
to
rev()
is in fact harmless, but it cannot be justified on the basis
of a general subtyping relation between
List<?>
and
List<T>
. The call is harmless, be-
cause the incoming argument is doubtless a list of some type (albeit an unknown one).
If we can capture this unknown type in a type variable
X
, we can infer
T
to be
X
. That
is the essence of capture conversion. The specification of course must cope with com-
plications, like non-trivial (and possibly recursively defined) upper or lower bounds,
the presence of multiple arguments etc.
Mathematically sophisticated readers will want to relate capture conversion to estab-
lished type theory. Readers unfamiliar with type theory can skip this discussion - or
else study a suitable text, such as
Types and Programming Languages
by Benjamin
Pierce, and then revisit this section.
Here then is a brief summary of the relationship of capture conversion to established
type theoretical notions. Wildcard types are a restricted form of existential types. Cap-
ture conversion corresponds loosely to an opening of a value of existential type. A
capture conversion of an expression
e
can be thought of as an
open
of
e
in a scope that
comprises the top level expression that encloses
e
.
The classical
open
operation on existentials requires that the captured type variable
must not escape the opened expression. The
open
that corresponds to capture conver-
sion is always on a scope sufficiently large that the captured type variable can never
be visible outside that scope. The advantage of this scheme is that there is no need
for a
close
operation, as defined in the paper
On Variance-Based Subtyping for Para-
metric Types
by Atsushi Igarashi and Mirko Viroli, in the proceedings of the 16th
European Conference on Object Oriented Programming (ECOOP 2002). For a formal
account of wildcards, see
Wild FJ
by Mads Torgersen, Erik Ernst and Christian Ples-
ner Hansen, in the 12th workshop on Foundations of Object Oriented Programming
(FOOL 2005).
5.1.11. String Conversion
Any type may be converted to type
String
by
string conversion
.
A value
x
of primitive type
T
is first converted to a reference value as if by giving it as an
• If
T
is
boolean
, then use
new Boolean(
x
)
.
• If
T
is
char
, then use
new Character(
x
)
.