A type is a set of values, plus an associated set of operations
valid on those values.
Types are useful for catching errors ("type-checking"), documenting
the programmer's intent, and to help the compiler generate better code.
Types in some languages (such as C) appear in programs,
but do not exist at run-time. In such languages, all type-checking
is done at compile-time. Other languages (such as standard Scheme)
do not have types as such, but they have predicates, which
allow you to check if a value is a member of certain sets; also,
the primitive functions will check at run-time if the arguments
are members of the allowed sets. Other languages, including Java
and Common Lisp, provide a combination: Types may be used as specifiers
to guide the compiler, but also exist as actual run-time values.
In Java, for each class, there is a corresponding java.lang.Class
run-time object, as well as an associated type (the set of values
of that class, plus its sub-classes, plus null
).
Kawa, like Java, has first-class types, that is types exist as
objects you can pass around at run-time. For each Java type,
there is a corresponding Kawa type (but not necessarily vice
versa). It would be nice if we could represent run-time
type values using java.lang.Class
objects, but unfortunately
that does not work very well. One reason is that we need
to be able to refer to types and classes that do not exist yet,
because we are in the processing of compiling them. Another
reason is that we want to be able to distinuish between different
types that are implemented using the same Java class.
Various Kawa constructs require or allow a type to be specified. Those specifications consist of type expressions, which is evaluated to yield a type value. The current Kawa compiler is rather simple-minded, and in many places only allows simple types that the compiler can evaluate at compile-time. More specifically, it only allows simple type names that map to primitive Java types or java classes.
These types are bound to identifiers having the form <TYPENAME>
.
(This syntax and most of the names are as in RScheme.)
To find which Java classes these types map into, look in
kawa/standard/Scheme.java
.
Note that the value of these variables are instances
of gnu.bytecode.Type
,
not (as you might at first expect) java.lang.Class
.
<number>
.
<quantity>
.
<complex>
.
<real>
.
<rational>
.
<list>
.
<symbol>
).
<object>
, in contrast to type <char>
, which is the
primitive Java char
type.
<java.lang.String>
(just as <symbol>
does).
However, coercing a value to <String>
is done by
invoking the toString
method on the value to be coerced.
Thus it "works" for all objects.
However, it does not work for #!null
. (Using
the fully-qualified <java.lang.String>
does work for #!null
,
but does not work for non-String
objects.)
More will be added later.
A type specifier can also be one of the primitive Java types.
The numeric types <long>
, <int>
, <short>
,
<byte>
, <float>
, and <double>
are converted from the
corresponding Scheme number classes. Similarly, <char>
can be converted to and from Scheme characters. The type
boolean
matches any object, and the result is false
if and only if the actual argument is #f
.
The return type <void>
indicates that no value is returned.
A type specifier can also be a fully-qualified Java class name
(for example <java.lang.StringBuffer>
). In that case,
the actual argument is cast at run time to the named class.
Also, <java.lang.StringBuffer[]>
represents
an array of references to java.lang.StringBuffer
objects.
<object>
.
Go to the first, previous, next, last section, table of contents.