Re: type and subtype
- From: Jeffrey Carter <spam@xxxxxxxx>
- Date: Sun, 24 Jul 2005 16:40:26 GMT
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_NumberM := 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 .
- Follow-Ups:
- Re: type and subtype
- From: Dmitry A. Kazakov
- Re: type and subtype
- References:
- type and subtype
- From: Douglas Chong
- Re: type and subtype
- From: Ludovic Brenta
- type and subtype
- Prev by Date: Re: type and subtype
- Next by Date: ++ of C in ada
- Previous by thread: Re: type and subtype
- Next by thread: Re: type and subtype
- Index(es):