home *** CD-ROM | disk | FTP | other *** search
/ Solo Programadores 22 / SOLO_22.iso / docs / faq / prog / part1.dos / text0000.txt < prev   
Encoding:
Text File  |  1996-04-29  |  31.8 KB  |  812 lines

  1. Archive-name: computer-lang/Ada/programming/part1
  2. Comp-lang-ada-archive-name: programming/part1
  3. Posting-Frequency: monthly
  4. Last-modified: 28 February 1996
  5. Last-posted: 26 January 1996
  6.  
  7.                                Ada Programmer's
  8.                        Frequently Asked Questions (FAQ)
  9.  
  10.    IMPORTANT NOTE: No FAQ can substitute for real teaching and
  11.    documentation. There is an annotated list of Ada books in the
  12.    companion comp.lang.ada FAQ.
  13.  
  14. This is part 1 of a 4-part posting.
  15. Part 2 begins with question 5.
  16. Part 3 begins with question 6.
  17. Part 4 begins with question 9.
  18. They should be the next postings in this thread.
  19.  
  20.     Recent changes to this FAQ are listed in the first section after the table
  21.     of contents (in part 1). This document is under explicit copyright.
  22.  
  23.  
  24. Introduction
  25.  
  26.    Ada is an advanced, modern programming language, designed and
  27.    standardized to support and strongly encourage widely recognized
  28.    software engineering principles: reliability, portability, modularity,
  29.    reusability, programming as a human activity, efficiency,
  30.    maintainability, information hiding, abstract data types, genericity,
  31.    concurrent programming, object-oriented programming, etc.
  32.  
  33.    All validated Ada compilers (i.e. a huge majority of the commercial
  34.    Ada compilers) have passed a controlled validation process using an
  35.    extensive validation suite. Ada is not a superset or extension of any
  36.    other language. Ada does not allow the dangerous practices or effects
  37.    of old languages, although it does provide standardized mechanisms to
  38.    interface with other languages such as Fortran, Cobol, and C.
  39.  
  40.    Ada is recognized as an excellent vehicle for education in programming
  41.    and software engineering, including for a first programming course.
  42.  
  43.    Ada is defined by an international standard (the language reference
  44.    manual, or LRM), which has been revised in 1995. Ada is taught and
  45.    used all around the world (not just in the USA). Ada is used in a very
  46.    wide range of applications: banking, medical devices,
  47.    telecommunications, air traffic control, airplanes, railroad
  48.    signalling, satellites, rockets, etc.
  49.  
  50.    The latest version of this FAQ is always accessible through WWW as
  51.    http://lglwww.epfl.ch/Ada/FAQ/programming.html#title
  52.  
  53. Maintenance
  54.  
  55.    This FAQ is maintained on an individual volunteer basis, by Magnus
  56.    Kempe (Magnus.Kempe@di.epfl.ch). [Note: This is done as a hobby, not
  57.    in my capacity as an employee at the Swiss Federal Institute of
  58.    Technology. --MK]
  59.  
  60.    The coding style used in most of the example Ada code is my own, and
  61.    you'll have to live with it (you may want to adopt it :-).
  62.  
  63.  
  64.      _________________________________________________________________
  65.  
  66.    Opinions (if any) expressed are those of the submitters and/or
  67.    maintainer.
  68.      _________________________________________________________________
  69.  
  70. Table of Contents:
  71.  
  72.      * 1: Recent changes to this FAQ
  73.  
  74.      * 2: Information about this document
  75.  
  76.      * 3: Elementary Questions
  77.           + 3.1: How do I make operations directly visible without
  78.             "use"ing a package?
  79.           + 3.2: How do I assign to an array of length 1?
  80.           + 3.3: How do I create a C-style nul-terminated string?
  81.           + 3.4: How can I create an array of strings of various length?
  82.           + 3.5: I know an exception is raised, but my program quits with
  83.             no warning. Why?
  84.           + 3.6: I have only one task in my program, but it doesn't seem
  85.             to run. Why?
  86.           + 3.7: How do I increase the stack size for a task?
  87.           + 3.8: What's the difference between a type conversion and a
  88.             qualifier?
  89.           + 3.9: How do I avoid the potential space in front of
  90.             Integer'Image?
  91.           + 3.10: Why is an exception raised when giving a default
  92.             discriminant?
  93.           + 3.11: When I want an Integer type, what's wrong with just
  94.             using the predefined type Integer or Long_Integer? Why would
  95.             I ever want to declare new Integer types?
  96.           + 3.12: Since I can always declare my own portable integer
  97.             types, why would I ever want to use the predefined type
  98.             Integer?
  99.           + 3.13: I am learning Ada. Can I experiment with a game
  100.             program?
  101.           + 3.14: How can I do a non-blocking, keystroke-at-a-time read
  102.             from the terminal?
  103.  
  104.  
  105.      * 4: Advantages of Ada
  106.           + 4.1: Why use Ada?
  107.           + 4.2: Ada seems large and complex, why is it this way?
  108.           + 4.3: Is there a contest with fame and money for good Ada
  109.             programmers?
  110.  
  111.  
  112.      * 5: Object-Oriented Programming with Ada
  113.           + 5.1: Why does Ada have "tagged types" instead of classes?
  114.           + 5.2: Variant records seem like a dead feature now. When
  115.             should I use them instead of tagged types?
  116.           + 5.3: What is meant by "interface inheritance" and how does
  117.             Ada support it?
  118.           + 5.4: How do you do multiple inheritance in Ada 9X?
  119.           + 5.5: Why are Controlled types so, well, strange?
  120.           + 5.6: What do "covariance" and "contravariance" mean, and does
  121.             Ada support either or both?
  122.           + 5.7: What is meant by upcasting/expanding and
  123.             downcasting/narrowing?
  124.           + 5.8: How does Ada do "narrowing"?
  125.           + 5.9: What is the difference between a class-wide access type
  126.             and a "general" class-wide access type?
  127.  
  128.  
  129.      * 6: Ada Numerics
  130.           + 6.1: Where can I find anonymous ftp sites for Ada math
  131.             packages? In particular where are the random number
  132.             generators?
  133.           + 6.2: How can I write portable code in Ada 83 using predefined
  134.             types like Float and Long_Float? Likewise, how can I write
  135.             portable code that uses Math functions like Sin and Log that
  136.             are defined for Float and Long_Float?
  137.           + 6.3: Is Ada any good at numerics, and where can I learn more
  138.             about it?
  139.           + 6.4: How do I get Real valued and Complex valued math
  140.             functions in Ada 95?
  141.           + 6.5: What libraries or public algorithms exist for Ada?
  142.  
  143.  
  144.      * 7: Efficiency of Ada Constructs
  145.           + 7.1: How much extra overhead do generics have?
  146.  
  147.  
  148.      * 8: Advanced Programming Techniques with Ada
  149.           + 8.1: How can I redefine the assignment operation?
  150.           + 8.2: Does Ada have automatic constructors and destructors?
  151.           + 8.3: Should I stick to a one package, one type approach while
  152.             writing Ada software?
  153.           + 8.4: What is the "Beaujolais Effect"?
  154.           + 8.5: What about the "Ripple Effect"?
  155.           + 8.6: How to write an Ada program to compute when one has had
  156.             too much alcohol to legally drive?
  157.           + 8.7: Does Ada have macros?
  158.  
  159.  
  160.      * 9: Ada and Other Programming Languages
  161.           + 9.1: Where can I find programs that will translate from [some
  162.             language] to Ada?
  163.           + 9.2: How can I convert Ada 83 sources to Ada 9X?
  164.           + 9.3: I hear that Ada is slower than Fortran or C, is that
  165.             true?
  166.           + 9.4: Isn't Ada less "elegant" than Eiffel?
  167.           + 9.5: Are there any papers detailing the differences between
  168.             Ada and C++?
  169.           + 9.6: I keep hearing that Ada is a "strongly typed language",
  170.             but it seems different from what's meant in C++. Are they
  171.             different?
  172.           + 9.7: I'm told Ada does all sorts of static type checking, but
  173.             can't you get the same effect using a tool like "lint" with
  174.             C?
  175.           + 9.8: Does Ada have something like the Standard Template
  176.             Library (STL) in C++, or like the components one finds in
  177.             Smalltalk environments?
  178.           + 9.9: Where can I find the equivalent of "printf" in Ada?
  179.  
  180.  
  181.      * 10: Interfacing with Ada
  182.           + 10.1: I am writing software that used the Distributed
  183.             Interactive Simulation (DIS) interface, does an interface
  184.             exist in Ada?
  185.           + 10.2: Is there any support for Common Object Request Broker
  186.             Architecture (CORBA) for Ada 9X?
  187.  
  188.  
  189.      * 11: Finding Additional Information
  190.           + 11.1: Where can I find Ada books?
  191.           + 11.2: Are there other Ada-related FAQs?
  192.           + 11.3: What is the "HBAP WWW Server"?
  193.  
  194.  
  195.      * 12: Pretty-printing and Measuring Ada Source Code
  196.           + 12.1: Is there software that generates a pretty PostScript
  197.             file from Ada source code?
  198.           + 12.2: I use vgrind to do "pretty printing" of my source. Is
  199.             there a vgrind definition for Ada?
  200.           + 12.3: How about a source code reformatter?
  201.           + 12.4: How can I count source lines of code (SLOC)?
  202.           + 12.5: Can I measure other things?
  203.  
  204.  
  205.      * 13: Credits
  206.  
  207.      * 14: Copying this FAQ
  208.  
  209.  
  210.      _________________________________________________________________
  211.  
  212. 1: Recent changes to this FAQ
  213.  
  214.      * 960228: references for redefining the assignment operation (8.1)
  215.        (switched questions 8.1 and 8.2).
  216.      * 960130: another game written in Ada (3.13).
  217.      * 960123: ARA contest for good Ada (4.3).
  218.      * 950925: removed a couple of non-programming questions.
  219.      * 950828: the change logs now indicate the section/question number.
  220.      * 950819: non-blocking, keystroke-at-a-time reading (3.14).
  221.      * 950819: some URL updates: AdaIC files keep moving around.
  222.      * 950630: corrected answers on task switching (3.6) and conversion
  223.        vs. qualifier (3.8).
  224.      * 950517: "general" class-wide access type (5.9).
  225.      * 950413: variant records vs. tagged types (5.2), submitted by
  226.        K.áShillington.
  227.      * 950406: why Ada does not and should not have macros (8.7).
  228.      * 950320: more on DIS (10.1).
  229.      * 950315: Ada is good at numerics (6.3), by J.áParker.
  230.      * 950309: drinking and driving example (8.6).
  231.      * 950306: added counting SLOC (12.4) and measuring (12.5).
  232.      * 950222: fixed some typos.
  233.      * 950207: revised introduction.
  234.      * 950202: updated discussion of C++ STL and Smalltalk library (9.8).
  235.      * 950126: advantages of code sharing for generics (7.1); Pascal to
  236.        Ada tool (9.1).
  237.      * 950125: why define new integer types (3.11), and why use the
  238.        predefined Integer type (3.12), submitted by J.áParker.
  239.      * 950124: approved for posting in *.answers.
  240.      * 950116: converting Ada 83 code to Ada 9X (9.2).
  241.      * 950106: lengthy code sections extracted and put on FTP server.
  242.      * 950105: printf solution (9.9); update on exception traces and
  243.        vgrind.
  244.      * 950104: links from TOC to all questions.
  245.  
  246.  
  247.    What's important and missing:
  248.      * everything, life, and 42
  249.  
  250.  
  251.      _________________________________________________________________
  252.  
  253.  
  254. 2: Information about this document
  255.  
  256.    This file is posted monthly to comp.lang.ada, comp.answers, and
  257.    news.answers.
  258.  
  259.    This document has a home on the Home of the Brave Ada Programmers
  260.    (HBAP) WWW Server, in hypertext format, URL
  261.    http://lglwww.epfl.ch/Ada/FAQ/programming.html
  262.  
  263.    It is available --as posted in *.answers-- on rtfm.mit.edu, which
  264.    archives all FAQ files posted to *.answers; see
  265.    ftp://rtfm.mit.edu/pub/usenet-by-group/news.answers/computer-lang/Ada
  266.  
  267.    The text-only version is also available in directory
  268.    ftp://lglftp.epfl.ch/pub/Ada/FAQ
  269.  
  270.    Magnus Kempe maintains this document; it's a hobby, not a job.
  271.    Feedback (corrections, suggestions, ideas) about it is to be sent via
  272.    e-mail to Magnus.Kempe@di.epfl.ch
  273.    Thanks.
  274.  
  275.    In all cases, the most up-to-date version of the FAQ is the version
  276.    maintained on the HBAP WWW Server. Please excuse any formatting
  277.    inconsistencies in the posted version of this document, as it is
  278.    automatically generated from the on-line version.
  279.  
  280.      _________________________________________________________________
  281.  
  282.  
  283. 3: Elementary Questions
  284.  
  285.  
  286. 3.1: How do I make operations directly visible without "use"ing the package?
  287.  
  288.    In Ada 83, you can rename the operations in your scope.
  289.  
  290.      -- Say you have an integer type called Int in package Types
  291.      function "<" (Left, Right : Types.Int)
  292.        return Boolean
  293.        renames Types."<";
  294.      -- Make sure the profiles of the first and last "<" match!
  295.  
  296.  
  297.    For operators, Ada 95 introduces the "use type" clause:
  298.  
  299.      use type Types.Int; -- makes operators directly visible
  300.  
  301.  
  302. 3.2: How do I assign to an array of length 1?
  303.  
  304.    Because of ambiguity of parentheses, named notation must be used for
  305.    one-element aggregates (or, under a different angle: a positional
  306.    aggregate must have more than one component).
  307.  
  308.    See [RM9X 4.3.3(7)] as well as the syntax rule of
  309.    positional_array_aggregate in [RM9X 4.3.3]; historians see [RM83
  310.    4.3(4)].
  311.  
  312.      declare
  313.        Array_of_One : array (1..1) of Float;
  314.      begin
  315.        -- Array_of_One := (10.0);   -- Won't work, parsed as an expression
  316.                                     -- within parentheses
  317.  
  318.        Array_of_One := (1 => 10.0); -- No ambiguity here
  319.      end;
  320.  
  321.  
  322.    You can't write a one-element positional aggregate in Ada. Nor a
  323.    zero-element aggregate. The reason for this restriction is that it
  324.    would be difficult for compilers to determine whether:
  325.  
  326.      ( exp )
  327.  
  328.    is a parenthesized expression of some type, or an aggregate of an
  329.    array type. If Ada had used some other notation for aggregates (say,
  330.    "[...]"), then this problem would not exist.
  331.  
  332.    Apparently the original requirements for Ada forbade using certain
  333.    ASCII characters, like '[' and ']', because those characters were not
  334.    available on all hardware. Also, certain characters are used for
  335.    different purposes and glyphs in countries that need additional
  336.    letters not present in ASCII.
  337.  
  338.  
  339. 3.3: How do I create a C-style nul-terminated string?
  340.  
  341.    In a declaration block, append an ASCII.NUL to create a constant Ada
  342.    string.
  343.  
  344.      declare
  345.        Str_Nul : constant String := Str & ASCII.NUL;
  346.      begin
  347.        Call_Requiring_C_String (Str_Nul (Str_Nul'First)'Address);
  348.      end;
  349.  
  350. -- or --
  351.  
  352.      function Nul_Terminate (Str : String)
  353.        return String is
  354.        Str_Nul : constant String := Str & ASCII.NUL;
  355.      begin
  356.        return Str_Nul;
  357.      end Nul_Terminate;
  358.  
  359.  
  360. 3.4: How can I create an array of strings of various length?
  361.  
  362.    In Ada 83, you have to use string access types and "new" to get
  363.    "ragged" arrays:
  364.  
  365.      type String_Access is
  366.        access String;
  367.  
  368.      Strings : constant array (Positive range 1..3) of String_Access
  369.              := ( 1 => new String'("One"),
  370.                   2 => new String'("Two"),
  371.                   3 => new String'("Three")
  372.                 );
  373.  
  374.  
  375.    In Ada 95, the process is simplified by using aliased constants:
  376.  
  377.      type String_Access is
  378.        access constant String;
  379.  
  380.      One : aliased constant String := "One";
  381.      Two : aliased constant String := "Two";
  382.      Three : aliased constant String := "Three";
  383.  
  384.      Strings : constant array (Positive range <>) of String_Access
  385.              := ( 1 => One'Access,
  386.                   2 => Two'Access,
  387.                   3 => Three'Access
  388.                 );
  389.  
  390.  
  391. 3.5: I know an exception is raised, but my program quits with no warning. Why?
  392.  
  393.  
  394.    On some Ada compilers, you have to manually "with" Text_IO before
  395.    exception information is diplayed to the terminal.
  396.  
  397.    On other Ada compilers, you must set an environment variable flag in
  398.    order to cause the exception information trace to be displayed.
  399.  
  400.  
  401. 3.6: I have only one task in my program, but it doesn't seem to run. Why?
  402.  
  403.    In Ada, the main procedure is automatically designated as a task. This
  404.    task may be running forever, thus starving your other task(s), because
  405.    round-robin scheduling (time-slicing) is not required (pre-emptive
  406.    scheduling applies to tasks with different levels of priority).
  407.  
  408.    If the task in question is getting starved, it's a programmer problem,
  409.    not an Ada problem. The programmer has to use an Ada compiler that
  410.    supports pragma Time_Slice, or do the scheduling himself (by changing
  411.    the implementation of his Ada program to ensure that no task starves
  412.    another).
  413.  
  414.    One solution is to explicitly put the main task to sleep within a loop
  415.    construct in order to avoid starvation of the other task(s), as in:
  416.  
  417.      procedure Main is
  418.        task Test;
  419.        task body Test is
  420.        begin
  421.          loop
  422.            delay 1.0;
  423.            Text_IO.Put_Line ("Test");
  424.          end loop;
  425.        end Test;
  426.      begin
  427.        loop
  428.          delay 20.0;
  429.          Text_IO.Put_Line ("Sleeping then writing");
  430.        end loop;
  431.      end Main;
  432.  
  433.  
  434. 3.7: How do I increase the stack size for a task?
  435.  
  436.    Define the task as a "task type" and then use a pragma representation
  437.    clause.
  438.  
  439.      task type A_Task_Type;
  440.      for A_Task_Type'STORAGE_SIZE use 10_000;
  441.      -- 10K bytes allocated to instances of A_Task_Type
  442.      A_Task : A_Task_Type;
  443.  
  444.  
  445. 3.8: What's the difference between a type conversion and a qualifier?
  446.  
  447.    Use a qualifier (tick) to tell the compiler what type it can expect;
  448.    this is strictly a compile-time issue: a qualifier "hints" the type,
  449.    usually to remove an ambiguity. Use a conversion to tell the compiler
  450.    to convert an expression from one type to another (usually within one
  451.    derivation hierarchy); this operation may require a change of
  452.    representation at run-time (e.g. in case of a representation clause
  453.    applying exclusively to the source type).
  454.  
  455.      A : Integer := Integer'(1);  -- this is a qualifier: same as ":= 1;"
  456.      B : Integer := Integer (1);  -- this is a conversion
  457.  
  458.  
  459. 3.9: How do I avoid the potential space in front of Integer'Image?
  460.  
  461.    Use the function Trim from package Ada.Strings.Fixed (you can
  462.    actually trim strings in many other useful ways):
  463.  
  464.      function My_Image (I : Integer)
  465.        return String is
  466.      begin -- My_Image
  467.        return Ada.Strings.Fixed.Trim (Integer'Image (I), Ada.Strings.Left);
  468.      end My_Image;
  469.  
  470.      ... My_Image (12) = "12" ...
  471.  
  472.  
  473.    In Ada 83, code a function that accepts a string and strips the
  474.    leading blank:
  475.  
  476.      function Strip_Leading_Blank (Str : String)
  477.        return String is
  478.      begin -- Strip_Leading_Blank
  479.        if Str (Str'First) = ' ' then
  480.          return Str (1+Str'First .. Str'Last);
  481.        else
  482.          return Str;
  483.        end if;
  484.      end Strip_Leading_Blank;
  485.  
  486.      ...
  487.  
  488.      function My_Image (I : Integer)
  489.        return String is
  490.      begin -- My_Image
  491.        return Strip_Leading_Blank (Integer'Image (I));
  492.      end My_Image;
  493.  
  494.      ... My_Image (12) = "12" ...
  495.  
  496.  
  497. 3.10: Why is an exception raised when giving a default discriminant?
  498.  
  499.    Let's assume you would like to model varying-length strings:
  500.  
  501.      type V_String (Size : Natural := 0) is
  502.        record
  503.          S : String (1 .. Size);
  504.        end record;
  505.  
  506.  
  507.    (from Robert Dewar)
  508.  
  509.    When you give a default discriminant, then one method (I actually
  510.    think it is the preferred method) of implementation is to allocate the
  511.    maximum possible length. Since your discriminant is of type Natural,
  512.    this clearly won't work!
  513.  
  514.    GNAT may compile it, but it won't run it, and indeed I consider it a
  515.    GNAT bug (on the todo list) that no warning is issued at compile time
  516.    for this misuse.
  517.  
  518.    Some compilers, notably Alsys and RR, have at least partially "solved"
  519.    this problem by introducing hidden pointers, but this to me is an
  520.    undesirable implementation choice.
  521.  
  522.    First, it means there is hidden heap activity, which seems
  523.    undesirable. In a language where pointers are explicit, it is
  524.    generally a good idea if allocation is also explicit, and certainly
  525.    for real-time work, hidden anything is worrisome.
  526.  
  527.    Second, it is not easy to do uniformly. Alsys ends up introducing
  528.    arbitrary restrictions on the composition of such types (try making an
  529.    array of them), and RR introduces non-contiguous representations,
  530.    which are legal but troublesome.
  531.  
  532.    To "solve" the problem yourself, just declare a reasonable maximum
  533.    length, and use a subtype representing this length as the subtype of
  534.    the discriminant:
  535.  
  536.      Max_Length : constant := 200;
  537.  
  538.      subtype Index is
  539.        Natural range 0 .. Max_Length;
  540.  
  541.      type V_String (Size : Index := 0) is
  542.        record
  543.          S : String (1 .. Size);
  544.        end record;
  545.  
  546.  
  547. 3.11: When I want an Integer type, what's wrong with just using the predefined
  548. type Integer or Long_Integer? Why would I ever want to declare new Integer
  549. types?
  550.  
  551.    If you declare 2 distinct integer types, for example,
  552.  
  553.      type Data_Index        is range 1..100;
  554.      type Time_Series_Index is range 0..2**15-1;
  555.  
  556.  
  557.    then objects of type Data_Index can't be assigned (directly) to
  558.    variables of type Time_Series_Index, and vice-versa. Likewise,
  559.    variables of these 2 types can't be mixed in arithmetical expressions
  560.    (without explicit type conversions). This may seem like a source of
  561.    endless irritation, but on the contrary, good progammers use it to
  562.    improve the clarity of their code, to make it more robust, and more
  563.    portable. The first 2 examples discuss this. The third example
  564.    discusses the declaration of machine-portable 32-bit integers.
  565.    Declaring objects of type Integer can be highly non-portable, and of
  566.    course type Long_Integer may not exist on some compilers.
  567.  
  568.     Example 1.
  569.  
  570.    Suppose you declare arrays using the above indices:
  571.  
  572.      type Time_Series is array (Time_Series_Index) of Float;
  573.      type Y_Axis_Data is array (Data_Index)        of Float;
  574.  
  575.      Measurement : Time_Series;
  576.  
  577.  
  578.    Now if you mistakenly try to iterate over one array with the index of
  579.    the other, the compiler can catch the error at compile time:
  580.  
  581.      for I in Data_Index loop
  582.         Sum := Sum + Measurement(I);  -- compilation error
  583.      end loop;
  584.  
  585.  
  586.     Example 2.
  587.  
  588.    This is lifted from Tucker Taft's brief introduction to Ada 95 in the
  589.    contributed papers section of the Ada World Wide Web homepage. Here
  590.    Tucker uses the Ada 95 unsigned integers, called modular types, in the
  591.    implementation of a protected type, which defines a disk control unit.
  592.    Modular types are integer types with "and", "or" and "xor" defined, so
  593.    systems programmers are likely to use them as bit masks. Just as the
  594.    array indices of the 2 arrays defined above are never meant to be
  595.    mixed, the modular integer types used to implement the disk control
  596.    unit are never meant to be mixed. To make sure the compiler enforces
  597.    this, they are declared as distinct types:
  598.  
  599.      type Flags   is mod 2**4;  -- a 4-bit flags field
  600.      type Control is mod 2**4;  -- A 4-bit control field
  601.  
  602.      Status_Mask  : constant Flags := 2#1001#;   -- Set first and last bits.
  603.      Status_Ready : constant Flags := 2#1000#;   -- Status = Ready
  604.  
  605.      Start_Xfr    : constant Control := 2#0001#; -- Initiate xfr command
  606.  
  607.  
  608.    Now if someone attempts to apply a Flag variable where a Control
  609.    variable should be used (or vice-versa) the compiler will catch the
  610.    error. This is especially important when the code is maintained by
  611.    programmers who did not write it.
  612.  
  613.     Remarks on Examples 1 and 2.
  614.  
  615.    1. Notice that in both examples the programmer was able to state his
  616.    intentions rather forcefully in the code - intentions that otherwise
  617.    might have been expressed much less forcefully in comment statements.
  618.    Because of Ada's strong typing model, the compiler was able to catch
  619.    errors at compile-time when the programmer's intentions were violated.
  620.  
  621.  
  622.    2. Notice also that the Integer declarations in the 2 examples are
  623.    machine portable, unlike Integer and Long_Integer. A compiler will
  624.    typically map these integer types onto the most efficient base type
  625.    that is available on the target machine.
  626.  
  627.     Example 3.
  628.  
  629.    Although the examples given above are good ones, it is not necessarily
  630.    a common practice to define a large number of distinct integer types.
  631.    In many cases it is appropriate to use (say) a 32-bit integer (or a
  632.    small number of such types) and declare appropriate subtypes of it
  633.    (them). To declare a portable 32-bit integer (or more accurately, the
  634.    most efficient integer that is at least 32-bits):
  635.  
  636.    type Int_tmp is range -2**31+1 .. 2**31-1;
  637.    type Integer_32 is range Int_tmp'Base'First..Int_tmp'Base'Last;
  638.  
  639.  
  640.    A compiler may reject this declaration if no suitable base type is
  641.    available, but this is rare. What happens is this: in order to
  642.    implement Int_tmp, the compiler chooses as the base type of Int_tmp an
  643.    integer type that is available on the target machine. This base type
  644.    is usually the most efficient integer that accomodates the range of
  645.    Int_tmp, which in turn is usually the machine's 32-bit integer. (It
  646.    might even be a 64-bit integer on some machines, in which case
  647.    Integer_32'Size = 64, and Integer_32'Last = 2**63-1. Maybe we should
  648.    not call it Integer_32!)
  649.  
  650.  
  651. 3.12: Since I can always declare my own portable integer types, why would I
  652. ever want to use the predefined type Integer?
  653.  
  654.    The language itself provides some guidance here. The predefined type
  655.    Integer is used by Ada in the implementation of a number of convenient
  656.    services. The following examples describe some of these services.
  657.    Notice that in most of the following examples, it is unlikely that it
  658.    will ever matter whether or not the predefined type Integer is
  659.    16-bits, 32-bits, 48-bits, or 64-bits.
  660.  
  661.    a) The exponentiation of X (written X**N) is defined by the language
  662.    for any floating point or integer X, provided N is of type Integer. (N
  663.    should be non-negative for integer X though.)
  664.  
  665.    b) Ada's predefined String type (really just a packed unconstrained
  666.    array of characters) uses an index of subtype Positive (i.e. type
  667.    Integer).
  668.  
  669.    c) The array index in the following "short-hand" array declaration is
  670.    implicitly defined to be type Integer:
  671.  
  672.      A : array(10..40) of Float;
  673.  
  674.  
  675.    d) The loop parameter I in the following for loop is implicitly
  676.    declared type Integer:
  677.  
  678.      for I in 10..40 loop
  679.       ...
  680.      end loop;
  681.  
  682.  
  683.    This application of type Integer is the one most likely to get you
  684.    into portability trouble. If you write: "for I in 1..2**17 loop", then
  685.    you get a constraint error on compilers that make Integer 16-bits,
  686.    because 2**17 is out of range of any Ada 16-bit integer.
  687.  
  688.  
  689. 3.13: I am learning Ada. Can I experiment with a game program?
  690.  
  691.    Of course.
  692.  
  693.    The Public Ada library (FTP wuarchive.wustl.edu) has a portable Ada
  694.    Tetris program in the languages/ada/misc/games directory. It uses
  695.    tasking, keyboard input, and ANSI screen graphics. Have fun!
  696.  
  697.    There is also "program Small", a tiny text adventure program, that you
  698.    can expand; it is documented at URL
  699.    http://lglwww.epfl.ch/Ada/Tutorials/Lovelace/small.htm
  700.  
  701.  
  702. 3.14: How can I do a non-blocking, keystroke-at-a-time read from the terminal?
  703.  
  704.    Use the procedure Text_IO.Get_Immediate [RM95 A.10.7(11)].
  705.  
  706.    If you don't have an Ada 95 compiler but have a POSIX binding, there
  707.    is a package using POSIX services that provides non-blocking,
  708.    keystroke-at-a-time access to the terminal. It is available by FTP in
  709.    file ftp://lglftp.epfl.ch/pub/Ada/FAQ/inkey.ada
  710.  
  711.      _________________________________________________________________
  712.  
  713. 4: Advantages of Ada
  714.  
  715. 4.1: Why use Ada?
  716.  
  717.    Think of it like this: We're the kid on the street corner, licking
  718.    that tasty ice cream cone on a hot summer day; an impish grin
  719.    decorates our face as we consume our cool confection. Meanwhile, other
  720.    kids gather round, noticing our pleasure. It matters not a whit that
  721.    they've just had a drink, or had their fill with supper -- they now
  722.    want ice cream. We offer no lecture on how good the ice cream is, we
  723.    simply demonstrate that we are happy, and let their memories carry
  724.    them to the nearest ice cream truck.
  725.  
  726.    (Sorry, I got a little carried away --DW).
  727.  
  728.  
  729. 4.2: Ada seems large and complex, why is it this way?
  730.  
  731.    (Robert Dewar, lead designer of the GNU Ada compiler, responds):
  732.  
  733.    During the Ada 9X development process we have often had fierce
  734.    arguments over the need to simplify proposals, and I pointed out some
  735.    time ago that the idea of simplicity is heavily overloaded:
  736.      * simple to implement
  737.      * simple to describe informally
  738.      * simple to describe formally
  739.      * results in simple programs
  740.      * simple to understand and/or remember
  741.      * short to describe
  742.  
  743.  
  744.    None of these goals are quite the same, and often they severely
  745.    conflict.
  746.  
  747.    If you listen to programming language design types, especially from
  748.    universities, they often have very little experience in programming,
  749.    and especially little experience in writing large delivered,
  750.    maintained software. That doesn't mean they know nothing about
  751.    programming languages, but it does tend to mean that their view of
  752.    complexity is skewed, and in particularly concentrates on the
  753.    simplicity of the language itself, rather than on the simplicity of
  754.    resulting programs.
  755.  
  756.    A lot of the creative tension in the 9X design process arose from this
  757.    same fundamental dichotomy. The design team tended to have a high
  758.    tolerance for language complexity (partly because they were very good
  759.    at understanding language details), but had a lot of experience in
  760.    actual large scale programming, and so their idea of simplicity was
  761.    biased heavily to simplifying Ada programs. The opposite voice,
  762.    worried about the simplicity of the language itself, represented by a
  763.    section of the DR's and ISO group (who, being a larger more diverse
  764.    group tended to reflect a wider view), considered that the design team
  765.    had gone too far in this direction. If you want to get a feel for the
  766.    transitions, look at the early versions of the 9X ILS, particularly
  767.    version 1.0.
  768.  
  769.    In retrospect, I think we came up with what is at least very close the
  770.    optimal balance. Tuck can speak for himself here more clearly than I
  771.    can speak for him, but I would guess that he and the other members of
  772.    the team recognize that you have to be able to sell the resulting
  773.    design as an acceptably simple whole, and thus must step back from the
  774.    most extensive proposals, while on the other hand, the more
  775.    conservative KISS sentiments were convinced to accept more features
  776.    than they originally felt comfortable with because of convincing
  777.    programming examples and discussions of resulting programming
  778.    complexity. The third wing of opinion ("I don't care what you think,
  779.    but if we can't implement it, then it's not much use!") was also
  780.    effectively fed in from the user-implementor teams.
  781.  
  782.    Is the result too complex? Time will tell, but I think the balance is
  783.    a successful blend.
  784.  
  785.  
  786. 4.3: Is there a contest with fame and money for good Ada programmers?
  787.  
  788.    Yes, they should enter the Ada Lovelace Programming Contest sponsored
  789.    by the Ada Resource Association (ARA).
  790.  
  791.    The Ada contest seeks to recognize the most readable, original,
  792.    reusable, and clear working Ada programs. Like the Ada programming
  793.    language, the contest is named in honor of the first programmer in
  794.    history, Lady Ada Lovelace.
  795.  
  796.    Every three months, the ARA will pay US$ 750 to the best Ada code
  797.    segment submitted. Submissions must be received by the 15th (midnight)
  798.    of the "contest month" and the award will be announced at the end of
  799.    the second month. A submission is made by emailing the source code to
  800.  
  801.         ara-contest@ocsystems.com
  802.  
  803.    The first contest closed December 15th, 1995. (The next contest month
  804.    is March 1996.)
  805.  
  806.    The rules and guidelines of the contest are available from the Ada
  807.    Contest WWW Home at http://lglwww.epfl.ch/Ada/Contest/announce.html
  808.  
  809.    This contest is open to all. Sharpen your designs, code, comments, and
  810.    demos; show the world how good and unobfuscated your Ada code is, and
  811.    win the prize!
  812.