Contents | Prev | Next | The T Language Specification Spring 2005 |
CHAPTER 3
Every expression written in the T programming language has a type that can be deduced from the structure of the expression and the types of the literals, variables, and methods mentioned in the expression. It is possible, however, to write an expression in a context where the type of the expression is not appropriate. In some cases, this leads to an error at compile time.
A specific conversion from type S to type T allows an expression of type S to be treated at compile time as if it had type T instead. In some cases this will require a corresponding action at run time to check the validity of the conversion.
For convenience of description, the specific conversions that are possible in the T programming language are grouped into several broad categories:
There are three conversion contexts in which conversion of expressions may occur. The term "conversion" is also used to describe the process of choosing a specific conversion for such a context. For example, we say that an expression that is an actual argument in a method invocation is subject to "method invocation conversion," meaning that a specific conversion will be implicitly chosen for that expression according to the rules for the method invocation argument context.
This chapter first describes the three categories of conversions (§3.1). Then the three conversion contexts are described:
Specific type conversions in the T programming language are divided into the following categories.
A conversion from a type to that same type is permitted for any type.
This may seem trivial, but it does have practical consequences. It is always permitted for an expression to have the desired type to begin with, thus allowing the simply stated rule that every expression is subject to conversion, if only a trivial identity conversion.
The following conversions are called the widening reference conversions:
Object
from any other class type.)
null
type to any class type or array type.
Object
.
Such conversions never require a special action at run time. They consist simply in regarding a reference as having some other type in a manner that can be proved correct at compile time.
See §5 for the detailed specifications for classes and §6 for arrays.
The following conversions are called the narrowing reference conversions:
Object
to any other class type.)
Object
to any array type.
Such conversions require a test at run time to find out whether the actual reference value is a legitimate value of the new type.
null
type to any primitive type.
null
type other than the identity conversion.
Object
.
Object
.
Assignment conversion occurs when the value of an expression is assigned (§8.16) to a variable: the type of the expression must be converted to the type of the variable. Assignment contexts allow the use of an identity conversion (§3.1.1) or a widening reference conversion (§3.1.2).
If the type of the expression cannot be converted to the type of the variable by a conversion permitted in an assignment context, then a compile-time error occurs.
If the type of an expression can be converted to the type of a variable by assignment conversion, we say the expression (or its value) is assignable to the variable or, equivalently, that the type of the expression is assignment compatible with the type of the variable.
A value of the null
type (the null
reference is the only such value) may be assigned to any reference type, resulting in a null
reference of that type.
Assignment of a value of compile-time reference type S (source) to a variable of compile-time reference type T (target) is checked as follows:
Object
, or a compile-time error occurs.
int
type.
See §5 for the specification of classes and §6 for arrays.
Method invocation conversion is applied to each argument value in a method or constructor invocation (§13.9, §13.12): the type of the argument expression must be converted to the type of the corresponding parameter. Method invocation contexts allow the use of an identity conversion (§3.1.1) or a widening reference conversion (§3.1.2).
Casting conversion is applied to the operand of the cast operator: the type of
the operand expression must be converted to the type explicitly named by the cast operator.
Casting conversion allows the use of an identity conversion (3.1.1), a widening reference
conversion (3.1.2), or a narrowing reference conversion (3.1.3).
Casting using narrowing reference conversion (3.1.3) will require a run-time check to see
if the cast is valid.
In the event that the cast is not valid the program will terminate with an appropriate error
message.
Casting from a reference type into the primitive type int
,
or from the primitive type int
into a
reference type is not allowed, and will cause a compile-time error.
Contents | Prev | Next | The T Language Specification, Version 2 Spring 2006 |
Author(s): Joseph Prendergast (§3.1-3.3), Spring 2006 CS712/CS812 class (edits, §3.4)