Idioms (How do I ... using Ada)

From AdaCommons

Jump to: navigation, search

If you know how other languages handle certain operations, but Ada's ways have you stumped, look for (better yet, offer) your answer here.

Contents

Convert between types

Since Ada's typing system is very strong, basic conversions between types is a must.

Between Numeric Types

The conversion rules are the same for all numeric types, fixed, float, and modular.

 declare
     type Custom_Integer is range -67 .. 172;
     type Movie_Rating is range 1 .. 10;
     A : Custom_Integer;
     B : Movie_Rating;
 begin
     A := 9;                -- 9 is within the range of Custom_Integer's type declaration, so this is valid
     B := Movie_Rating (A); -- if A is outside of Movie_Rating's range, this will raise Constraint_Error
 end;

Between Enumerations

Strings and Characters

Occasionally one might need to work with Characters which are, or Strings which contain, control (or other special) characters. This section describes how Ada deals with these special cases.

Type conversions

Getting the position (ASCII code, number) of a character

Use the 'Pos attribute of the Character type, which returns an Integer.

 Character'Pos('A');

Packages Standard.ASCII and Ada.Latin_1

The package Standard.ASCII is considered obsolete in Ada 2005. The correct package to use is Ada.Characters.Latin_1. The package contains constants corresponding to values in the Latin-1 character set.

As an example, a program that prints HTTP headers might use the Latin_1 package to obtain values for line terminators:

with Ada.Characters.Latin_1;
with Ada.Text_IO;
 
LF : constant Character renames Ada.Characters.Latin_1.LF;
CR : constant Character renames Ada.Characters.Latin_1.CR;
 
Ada.Text_IO.Put ("Content-type: text/html" & CR & LF & CR & LF);

Character Literals

Checking for an Apostrophe (')

While an apostrophe character literal may be written in the form of three successive apostrophes ('''), so a Character or String might be queried for an apostrophe, as follows:

   -- Character literal.  This works, but probably isn't the clearest expression.
   if a_Character      = ''' then ...
   if a_String (Index) = ''' then ...
 
   -- Character constant. Requires Ada.Characters.Latin_1, but is expressed more clearly.
   Apostrophe : Character renames Ada.Characters.Latin_1.Apostrophe;
   if a_Character      = Apostrophe then ...
   if a_String (Index) = Apostrophe then ...

String Literals

Using Quotes

Double quote characters are placed in string literals by simply using two double quote characters:

X : constant String := "Hello ""World"""; -- Quotes may be embedded directly into strings by using two successive quotes within the string.
 
-- Character literal.
Quote : Character renames Ada.Characters.Latin_1.Quote;
Y     : constant String := "Hello " & Quote & "World" & Quote;

Printing the either X or Y above to standard output would result in:

Hello "World"

Records

Creating a constant record with default values

This is an Ada2005 feature. Also note that there seems to be a problem with this notation when using bounded strings in your record. The bounded strings must be explicitly set, then others => <> works.

 type Counter is record
   One   : Integer  := 42;
   Two   : Boolean  := False;
   Three : Duration := 19.58;
 end record;
 Default_Counter : constant Counter := (others => <>);

Flow Control

Abnormal program exit

In order to exit a program from a call level deeper than that of the main subprogram, a new exception (perhaps 'abnormal_Exit') may be declared. When an exit is desired, this exception may be raised and control will then be transferred to the exception handler of the main subprogram.

When using this method, beware of any 'when others => ...' clauses in existing exception handlers. These, of course, will catch the abnormal_Exit exception, and so render it useless (unless the 'when others' clause re-raises the exception).

The appropriate action to take, when handling the exception in the main subprogram, is application specific, but generally consists of an orderly shutdown of any allocated resources. In the case where no such resources have been allocated, a simple 'null;' may suffice.

External links

You can learn how to do typical tasks in Ada at these sites:

Personal tools