From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,703c4f68db81387d X-Google-Thread: 109fba,703c4f68db81387d X-Google-Thread: 115aec,703c4f68db81387d X-Google-Thread: f43e6,703c4f68db81387d X-Google-Attributes: gid103376,gid109fba,gid115aec,gidf43e6,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!news.glorb.com!wn13feed!worldnet.att.net!bgtnsc05-news.ops.worldnet.att.net.POSTED!53ab2750!not-for-mail Newsgroups: comp.lang.ada,comp.lang.c++,comp.realtime,comp.software-eng Subject: Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada) From: Jim Rogers References: <4229bad9$0$1019$afc38c87@news.optusnet.com.au> <1110032222.447846.167060@g14g2000cwa.googlegroups.com> <871xau9nlh.fsf@insalien.org> <3SjWd.103128$Vf.3969241@news000.worldonline.dk> <87r7iu85lf.fsf@insalien.org> <1110052142.832650@athnrd02> <1800709.LegSC0zdoW@linux1.krischik.com> <1110099035.843154@athnrd02> Followup-To: comp.lang.ada,comp.lang.c++ User-Agent: Xnews/5.04.25 Message-ID: Date: Sun, 06 Mar 2005 16:13:40 GMT NNTP-Posting-Host: 12.73.182.133 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc05-news.ops.worldnet.att.net 1110125620 12.73.182.133 (Sun, 06 Mar 2005 16:13:40 GMT) NNTP-Posting-Date: Sun, 06 Mar 2005 16:13:40 GMT Organization: AT&T Worldnet Xref: g2news1.google.com comp.lang.ada:8758 comp.lang.c++:44347 comp.realtime:1022 comp.software-eng:4554 Date: 2005-03-06T16:13:40+00:00 List-Id: Ioannis Vranos wrote in news:1110099035.843154@athnrd02: > 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