New Numeric Types
- In java we have one way to create a new type: create a new
class
- (Java 1.5 has added two new ways to create a new type:
enums and parameterized types)
- A class can inherit features from a parent type and all
classes inherit from class Object. Thus new types are created from
existing types.
- New types in Java are subtypes
- Ada has a richer type model. For numeric types:
- Two ways to create numeric subtypes:
- subtype
- numeric derived type
- You can also create NEW numeric subtypes:
- New numeric types are NOT subtypes
- Ada's type model also allows creation of subtypes that are not
numeric. We will see this later.
Remember Subtypes
- Subtypes can be used in place of parent types.
- Runtime errors occur if invalid value used
- Subtypes allow more expressive programs
- Example:
procedure subs is
subtype Month_Day is Integer range 1 .. 31;
d: Month_Day := 1;
i: Integer;
j: Integer := 33;
t: Temperature;
begin
i := d; -- Always valid
d := i; -- May be valid
d := j; -- Raises Constraint Error
d := 34; -- Compiles with warning. Would raise Constraint Error
end subs;
Notes
What is a Type?
- Remember - a type consists of two sets:
- a set of values
- a set of primitive operations
- An Ada subtype has a subset of the values of the parent type but
the same set of operations!
Primitive Operations
- The primitive operations of a type are certain procedures
and functions that have one or more parameters of that type.
- Later we will see exactly which routines are the primitive operations.
Derived Types
- An Ada derived type is different way to create a type that has a
subtype relation with its parent type.
- subtypes and derived types essentially have an inheritance
relation
- derived types inherit all of the primitive
operations of the parent type,
however, they can NOT be used any place the parent type can
subtype subt is integer range 1 .. 100;
type deriv is new integer range 1 .. 100;
s: subt;
d: deriv;
i: integer;
begin
i := s; -- Okay
i := d; -- Compile error
In java, a parent type variable can automatically hold (ie point
to) a subtype object, but not so here: subtype okay, derived yes.
Note subtype has two meanings: ada and general OO
Main difference is operations automatically available
We ignore derived types for now
What is a Type?
- Remember - a type consists of two sets:
- a set of values
- a set of primitive operations
- When we create a new type we create two new sets!
Creating New Types
- In java we define a new types by creating a class
- In ada, we can create new type by simply saying
type XXX is YYY;
- XXX is the new name and YYY is the definition
- We can create many different kinds of new types:
- Numeric
- Enumerated
- Derived Types
- Arrays
- Records
- Pointers
- Tagged (allows classwide types and polymorphism)
- ...
- We will look at most of these
Creating a New Numeric Types
I/O with New Numeric Types
Arithmetic with New Numeric Types
- Operations: Arithmetic and relational operators are automatically
defined on new numeric types.
- Example: types3.adb
- You can think of operations as being inherited
New Types vs Subtypes
Key Issues
type XXX is Range X .. Y;
declares a distinct
numeric type
- Operations:
Arithmetic and relational operators, assignment, and conversions
are automatically defined for new types
- Operations: I/O NOT automatically defined for new types, but must be
explicitly defined
- Values: Distinct types cannot be mixed in assignment or operations
without conversions