Types, Subtypes, and Type Checking
What is a Type?
- Set of Values + Set of Operations
- Examples - Java: numeric, ...
- Examples - Ada: numeric, ...
What is a Subtype?
- If S is a subtype of T, then
- S has a subset of the values of T
- S has the operations of T
Example Subtypes
- Some example numeric subtypes in Ada:
-
subtype Natural is Integer range 0 .. Integer'last
-
subtype Positive is ...
-
subtype Test_Scores is ...
- All integer operations (eg :=, +, -, 'first, 'last, integer_text_io.put) apply
- Question: Why only 231-1 integers and not twice that?
- FYI: subtypes Positive and Natural are defined in package
Standard (along with lots of other stuff).
Subtypes and Type Checking Assignment
- Only valid values can be assigned to a subtype variable
- Examples:
declare
n: Natural := 100;
i: Integer;
begin
i := n; -- does this work? (what kind of conversion)
n := i; -- does this work?
get(i);
n := i; -- does this work?
Runtime Type Check
- For the assignment in this code:
declare
n: Natural := 100;
i: Integer;
begin
get(i);
p := i; -- what happens here?
Compiler will generate code to do something like the following
if i < 0 then
raise Constraint_Error;
else
p := i;
end if;
Explicit conversions are allowed but not required
- What would that look like?
Subtypes in Java
- Numeric types: byte and int
int i;
byte b = s.nextByte();
i = b; // what kind of conversion?
i = 10;
// b = i; // Compile error - what kind of conversion
// how do we fix the assignment above?
Java Class Subtypes
- Subtypes also occur with Inheritance
- Assume class
Child inherits from class Parent:
class Parent{...}
class Child extends Parent{...}
Parent p;
Child c = new Child();
p = c; // Does this work? If not, how to fix?
c = p; // Does this work? If not, how to fix?
p = new Parent();
c = p; // Does this work? If not, how to fix?
Compiler generates code to check value at runtime
Subtypes and Type Checking Arithmetic
- Ada and Java: Numeric subtypes can be mixed
- Examples:
byte b;
int i;
i = i + b;
i: Integer;
n: Natural;
i := i + n;
What happens in these cases?
Bottom Line on Using Subtypes
- A variable whose type is subtype of a parent type can be used anywhere a variable of the parent type is expected
- Sometimes a cast is needed
- In ITEC 324 you learn that this the Liskov Substitution Principle
- Variables of a given type can only hold values of that type
- This is reinforced by a combination of compiler and runtime checks
- The type cast is the programmers way of telling the compiler,
I know that there might be a problem here
- go ahead and do the assignmnet.
- The compiler says okay, but I'll generate a RT value check, just in case
Motivation for Subtypes
- Some operations are easier
- Example:
if i in natural then ...
- Better type checking, with easy mixing of types and subtypes in arithmetic
expressions
- Allows programmer to write a the level of the problem
- can define and use differnt subtypes instead of using integers for everything
- Later we will see other ways of creating types such as new numeric types and
enumerated types