www.delorie.com/gnu/docs/gcc/gnat_ug_120.html   search  
 
Buy the book!


Untitled Document

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.3.5 Variables

A variable may be declared at the project file level, or in a package. Here are some examples of variable declarations:

 
   This_OS : OS := external ("OS"); --  a typed variable declaration
   That_OS := "Linux";              --  an untyped variable declaration

A typed variable declaration includes the variable name, followed by a colon, followed by the name of a string type, followed by :=, followed by a simple string expression.

An untyped variable declaration includes the variable name, followed by :=, followed by an expression. Note that, despite the terminology, this form of "declaration" resembles more an assignment than a declaration in Ada. It is a declaration in several senses:

A string variable declaration (typed or untyped) declares a variable whose value is a string. This variable may be used as a string expression.
 
   File_Name       := "readme.txt";
   Saved_File_Name := File_Name & ".saved";

A string list variable declaration declares a variable whose value is a list of strings. The list may contain any number (zero or more) of strings.

 
   Empty_List := ();
   List_With_One_Element := ("-gnaty");
   List_With_Two_Elements := List_With_One_Element & "-gnatg";
   Long_List := ("main.ada", "pack1_.ada", "pack1.ada", "pack2_.ada"
                 "pack2.ada", "util_.ada", "util.ada");

The same typed variable may not be declared more than once at project level, and it may not be declared more than once in any package; it is in effect a constant or a readonly variable.

The same untyped variable may be declared several times. In this case, the new value replaces the old one, and any subsequent reference to the variable uses the new value. However, as noted above, if a variable has been declared as a string, all subsequent declarations must give it a string value. Similarly, if a variable has been declared as a string list, all subsequent declarations must give it a string list value.

A variable reference may take several forms:

A context may be one of the following:

A variable reference may be used in an expression.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003