Friday, March 30, 2007

TYPES, VALUES AND VARIABLES(8)

TYPES, VALUES AND VARIABLES(8)

TYPE ERASURE:
Type erasure is a mapping from types (possibly including parameterized types and type variables) to types (that are never parameterized types or type variables). We write |T| for the erasure of a type T.
Type erasure is defined as:
- erasure of a parameterized type G as |G|
- erasure of a nested type T.C as |T|.C
- the erasure of an array type T[] as |T|[]
- the erasure of a type variable is the erasure of its leftmost bound. For example, if
T extends M implements N, then its erasure M and if
T implements N, then its erasure is N.
- erasure of every other type in the type itself.
The erasure of a method signature, m(T t) is m and erasure of T.

REIFIABLE TYPES:
Types that are completely available at run-time, after erasures, are known as reifiable types. A type is reifiable if and only if:
- it is a non-reifiable type.
- If parameterized, it has unbounded wildcard as type argument.
- It is an array type whose component type is reifiable.
- It is a raw type
- It is a primitive type

RAW TYPES:
A raw type is a parameterized type derivation to facilitate interfacing with non-generic legacy code. It is either:
a. the name of a generic type declaration less the actual type arguments i.e S has as raw type, S.
b. any non-static type member of a raw type that is not inherited from a superclass or superinterface of R.

it’s important to note the discussion in the jls for §4.8, particularly the fact that it is better to insert the complete generics for a member of a generic type or the type itself when referring to them, but in interfacing to legacy code, check your erasures very well.

INTERSECTION TYPES:
An intersection type takes the form T1 … Tn, for positive n, n>0, where each T, Ti, is defined by 1≤ i ≤n , are type expressions. Intersection types arise in the processes of capture conversion[1] and type inference[2].

Realise that intersection types are not written directly as part of a program.

Members of an intersection type:
1. a class or interface type without any member and without any inherited members from a superclass is an intersection type. Its values should be null.
2. a superclass, Ck, for a class type, T, such that Ck, has a superclass, Ci, but Ck and Ci have the same and equivalent members.
3. interfaces implemented by a class type are intersection types.

SUBTYPING:
The subtypes and supertype relations are all binary relations on types.

A direct supertype relationship is written as >1 if S is a direct supertype of T, then S >1 T.
S :> T indicates that a supertype relationship exists between S & T. S is a proper supertype of T, written S > T, if S :> T and S ≠ T.

The subtype relationship is denoted by <: . We write T <: S to denote T has a subtype relationship with S. T is a proper subtype of S, written T < S if T <: S and S ≠ T.
T is a direct subtype of S, written as T <1 S if S >1 T.

Note: subtyping does not extend through generic types: T <: U does not imply C <: C.

Subtyping among primitive types:
double >1 float
float >1 long
long >1 int
int >1 char
int >1 short
short >1 byte

Subtyping among class and interface types:
if we have a declaration like this:

Given parameterized type, C where C is defined by C , the two argument types have ranges that are subtypes of their corresponding bounds. That is T1 is bound by an upper bound, B1 and T2 by a lower bound, B2.

Given C, the direct supertype of this parameterized type are all of the following:
1. the direct superclass of the erasure of the type or C
2. the direct superinterface of C
3. the type Object, if C is an interface type with no direct superinterface (i.e it has no declaratioin of the form: implements T ).
4. the raw type, C.

package api_package;

public class Nigerian {}

//the container type or supertype of Lagosian is
//Nigerian
public class Lagosian extends Nigerian{
public static void main(String[] args){
Lagosian you = new Lagosian();

//I can cast succesfully to the supertype
System.out.println((Nigerian)you);
}
}

Given a type C its direct supertype is D if:
1. D is a direct supertype of C and theta represents each C within each “cage” of D.
Example:
package api_package;

public class Nigerian {
String name="tunde";
}
//the container type or supertype of Lagosian is
//Nigerian
public class Lagosian
extends Nigerian{
}
public class Loader {

public static void main(String[] args) {
Lagosian you = new Lagosian();

//Lagosian can access yoruba
System.out.println(you.name);
}
}
2. C is a supertype where S1 contains T1 and S2 contains T2 respectively.

The direct supertypes of an intersection type are the members of the intersection type.
The direct supertype of a type variable are the types listed in its bound.
The direct supertype of the null type are all reference types other than the null type itself.

Subtyping among Array types:
The following rules define the direct subtype relation among array types.

1. if S & T are both reference types, then
- S[] >1 T[], iff S >1 T.
- Object >1 Object[]
- Cloneable >1 Object[]
- java.io.Serializable >1 Object[]

2. if p is a primitive type, then
- Object >1 p[]
- Cloneable >1 p[]
- java.io.Serializable >1 p[]

Footers:
[1]. Capture conversion, to be discussed later, denotes a conversion of generics to their equivalent type variables taking into account the superclasses and subclasses that the type variable will have a contract with.
[2]. This is a process of inferring type arguments from method and constructor invocations.

No comments: