Re: type and subtype



Ludovic Brenta wrote:

Two subtypes of the same type are compatible with one another. One can convert between them using an implicit type conversion (i.e. no special syntax required).

There is no type conversion between subtypes of the same type, since they are the same type. There are checks that the value of the source matches the constraints of the destination.


Consider:

type Year_Number is range 1901 .. 2099;
type Month_Number is range 1 .. 12;

Y : Year_Number := 2000;
M : Month_Number := 6;

procedure P (Y : in Year_Number);

....

Y := M; -- illegal, different types
P (Y => Y + M); -- illegal, no such "+"

and then:

subtype Year_Number is Integer range 1901 .. 2099;
subtype Month_Number is Integer range 1 .. 12;

Y : Year_Number := 2000;
M : Month_Number := 6;

procedure P (Y : in Year_Number);

....

Y := M; -- legal, same type
P (Y => Y + M); -- legal, "+" (Left, Right : Integer) return Integer;
                -- Y + M in Year_Number

M := 12;
M := 2 * M - M; -- legal, operations evaluated for type (Integer)
                -- result in Month_Number

--
Jeff Carter
"I like it when the support group complains that they have
insufficient data on mean time to repair bugs in Ada software."
Robert I. Eachus
91
.