Download Ada in Action

Transcript
Chapter 2. Numeric Considerations
17
Figure 2. Distinct types.
------------------------------------------------------------- Each declared type is distinct, even if they have the
-- same name and definition as another integer type and are
-- in the same file.
-- In the example below, the two packages X and Y, and the
-- procedure P, are in a file called XYP.ada.
--
XYP.ada
package X is
type Whole_numbers is range -32768..32767;
procedure Fool_With(N : in out Whole_numbers);
end X;
package Y is
type Whole_numbers is range -32768..32767;
procedure Produce(Z : out Whole_numbers);
end Y;
with X, Y; use X, Y;
procedure P is
type Whole_numbers is range -32768..32767;
W : Whole_numbers;
begin
Produce(W);
-- this is line 28
Fool_With(W); -- this is line 29
end P;
---------------------------------------------------When you compile the file XYP.ada above, here's what
you get:
Meridian AdaVantage(tm) Compiler [v1.5 Apr 3, 1987] Target
8086
Package x added to library.
Package y added to library.
"XYP.ada", line 28: <<error>> identifier has wrong type "w"
"XYP.ada", line 29: <<error>> identifier has wrong type "w"
31 lines compiled.
2 errors detected.
6 miles. That often isn't enough. Programs that use the POOR_COORDINATES package may work on a
VAX, but not an 8086 because the number of bits in the predefined integer type is machine dependent.
The Ada Language Reference Manual allows compilers to predefine families of integer data types with
names like integer, long_integer, long_long_integer, short_integer, and short_short_integer, but it doesn't
say how many bits to use. A long_integer on an IBM PC has the same number of bits as an integer on a
VAX.
2.2.
STANDARD_INTEGERS package
Whenever it matters how many bits there will be in an integer, you should always specify the range
yourself. If you try to do this locally in your program, then you might have some trouble interfacing with a
Ada in Action