J
Jim Rogers
The one remaining, does it support namespaces? What subset of the
procedural paradigm it does not support?
Ada support the concept of namespaces, but there is no reserved word
"namespace". Ada uses packages to provide encapsulation and namespace.
Packages have been part of the Ada language since its earliest version.
Packages normally appear in two parts. The package specification defines
the interface to the package. The package body contains the
implementation
of all subprograms, tasks, and protected objects declared in the
specification.
Here is a specification for a generic (aka template) package:
generic
type Target_Type is private;
Target_Size : Natural;
package Bit_Utils is
procedure Show_Bits(Item : Target_Type);
end Bit_Utils;
We know this is a generic package because of the reserved word
"generic". The two lines following "generic" define the
generic formal parameters for this package. In this case the
first parameter is a type and the second parameter is an
integer with a minimum value of 0. The package provides an
interface to a single procedure (similar to a function
returning void in C++) named Show_Bits that takes a
single parameter of the same type as the generic formal
type.
The body of this package is:
with Ada.Text_Io;
with Ada.Integer_Text_Io;
with System;
package body Bit_Utils is
type Bits_Array is array(Positive range <>) of Boolean;
pragma Pack(Bits_Array);
type Byte is mod 2**8;
type Byte_Array is array(Positive range <>) of Byte;
package Mod_Io is new Ada.Text_IO.Modular_IO(Byte);
procedure Show_Bits(Item : Target_Type) is
Bit_View : Bits_Array(1..Target_Size);
for Bit_View'Address use Item'Address;
Byte_View : Byte_Array(1..Target_Size / Byte'Size);
For Byte_View'Address use Item'Address;
begin
for I in Byte_View'range loop
Mod_Io.Put(Item => Byte_View(I), Width => 4);
end loop;
Ada.Text_IO.New_Line(2);
for I in Bit_View'range loop
Ada.Integer_Text_Io.Put(Item => Boolean'Pos(Bit_View(I)),
Width => 1);
if I mod System.Storage_Unit = 0 then
Ada.Text_IO.New_Line;
end if;
end loop;
end Show_Bits;
end Bit_Utils;
The first 3 lines begin with the reserved word "with".
Those lines declare a dependency upon the compilation units
following the word "with". In this case all three
compilation units are pre-defined packages.
We and the compiler know that this is a package body because
of the use of the phrase "package body" on the line 4.
This package body contains the implementation of the procedure
Show_Bits. It also contains three type definitions and the
instantiation of the generic package Ada.Text_IO.Modular_IO for
type Byte. Since none of these types are delcaled in the
package specification, they are invisible to any calling
entity. They are equivalent to C++ private types.
This package was used in a program with the following "main"
procedure:
with Bit_Utils;
with Ada.Text_Io;
procedure Bit_Output is
type My_Type is
record
Name : String (1 .. 4);
Age : Positive;
Weight : Long_Float;
end record;
package My_Bits is new Bit_Utils(My_Type,
My_Type'Size);
package Flt_Bits is new Bit_Utils(Long_Float,
Long_Float'Size);
Mt : My_Type := ("Jim ", 55, 0.45435);
D : Long_Float := 0.45435;
begin
Ada.Text_Io.Put_Line("Output of My_Type");
My_Bits.Show_Bits(Mt);
Ada.Text_Io.Put_Line("Output of Long_Float");
Flt_Bits.Show_Bits(D);
end Bit_Output;
The procedure Bit_Output serves the same purpose as does
"main" in C++. Note that Bit_Output is preceeded by two
context clauses. One declares a dependency on the Bit_Utils
package and the other declares a dependency on the
Ada.Text_IO package. Ada does not use a pre-processor.
The package Bit_Utils is instantiated for two different types
inside Bit_Output. Each instantiation is given a unique name,
prividing a unique namespace.
Also I saw that under severe constraints the run-time safety of the
language is better to be switched off.
If the program has been proved to be correct without the safety elements,
and there is a sufficient performance improvement, yes you should turn
off the run-time safety elements. You have control of the elements you
want to turn of through simple pragma statements.
I do not know much on the language, but can one define general-purpose
containers with Ada's generics that do range checking and throw an
exception when there is an attempt to access outside the boundaries of
the container, even if the aforementioned run-time safety is switched
off?
Yes you can. You can always program in your own checks manually.
The drawback of doing that is that you cannot simply turn them off
with a pragma, nor can the compiler so effectively optimize those
checks out of your program when they are unneeded.
Jim Rogers