comp.lang.ada
 help / color / mirror / Atom feed
From: adam@irvine.com (Adam Beneschan)
Subject: Re: ada and robots
Date: 1997/06/20
Date: 1997-06-20T00:00:00+00:00	[thread overview]
Message-ID: <5oeijc$smf$1@krusty.irvine.com> (raw)
In-Reply-To: EBxtAs.3xK@world.std.com


bobduff@world.std.com (Robert A Duff) writes:

 >>>*      Ada does not support variable length parameter lists.
 >
 >Granted.  IMHO, a "nice to have" feature, but certainly nothing to do
 >with why C is better or worse at accessing low-level hardware
 >functionality.  It was left out of Ada to simplify the language.

I haven't been following this thread, so my apologies if I post
something that duplicates something someone else has written.

I don't miss having variable length parameter lists, because there are
a couple easy ways to accomplish practically the same thing.  One
technique is to declare a routine something like this:

    function Build_String (Input1, Input2, Input3, Input4, 
                           Input5, Input6, Input7, Input8 : String := "")
                return String;

Now, I have a way to make variable-length parameter calls, up to eight
parameters:

    Text_IO.Put_Line (Build_String ("a", "b"));
    Text_IO.Put_Line (Build_String ("a", "b", "c", "d", "e"));

etc.  (Of course, this type of solution assumes that you'll never
legitimately want to use "" as a parameter.)

Another solution, which works better if the parameter types are
scalars, is to use an array as a parameter.  I've written code like
this: 

    type Option_Type is (Allow_Hex, Allow_Underscore,
                         No_Decimal_Point, ...)
    type Option_Array is array (natural range <>) of Option_Type;
    procedure Parse_String (Source  : in string;
                            Options : in Option_Array;
                            etc.)

and now I can call

    Parse_String (S, (Allow_Underscore, No_Decimal_Point));
    Parse_String (S, (Allow_Underscore, No_Decimal_Point, Allow_Hex));

putting as many options as I like in the array parameter.  (C doesn't
allow you to create an in-line array like this to pass to a function,
or even to assign to an array variable.  In fact, I'm surprised the
anti-Ada poster gave us something like

 >>>C Code: 
 >>>
 >>>printf("\n\n The roller outputs for Roll[%d] are:\n Torque-%f\nVelocity-%f\
 >>>nForce- %f\n", roll, torque, velocity, force);
 >>>
 >>>Ada Code:
 >>>
 >>>Ada.Text_IO.New_Line(2);
 >>>Ada.Text_IO.Put("The roller outputs for Roll[");
 >>>Ada.Int_IO.Put(roll);
 >>>Ada.Text_IO.Put("] are:");
 >>>Ada.Text_IO.New_Line;
 >>>Ada.Text_IO.Put("Torque-");
 >>>Ada.Real_IO.Put(torque);
 >>>Ada.Text_IO.New_Line;
 >>>Ada.Text_IO.Put("Velocity-");
 >>>Ada.Real_IO.Put(velocity);
 >>>Ada.Text_IO.New_Line;
 >>>Ada.Text_IO.Put("Force-");
 >>>Ada.Real_IO.Put(force);
 >>>Ada.Text_IO.New_Line;
  
when it's so easy to answer:

C code:

   array_variable[0] = 7;
   array_variable[1] = x + 1;
   array_variable[2] = x + 2;
   array_variable[3] = (x - y) % 5;
   array_variable[4] = -3;

Ada code: 

   Array_Variable := (7, X + 1, X + 2, (X - Y) mod 5, -3);

)

I've even used this technique to write an Ada 83 version of printf().
Of course, you can't declare an array whose elements aren't the same
type, but all you have to do is declare a variant record type that
could hold any type you would want to output:

    type String_P is access string;
    type Data_Type is (V_Integer, V_Float, V_String);
    type Heterogeneous_Data (DType : Data_Type) is record
        case Dtype is
            when V_Integer =>   Int_Data    : Integer;
            when V_Float   =>   Float_Data  : Float;
            when V_String  =>   String_Data : String_P;
        end case;
    end record;

Now, declare an overloaded function that converts any data type to
Heterogeneous_Data.  Some people like overloading unary "+" for this.
Declare an array of Heterogeneous_Data, and use it as the parameter to
your Printf.  This does take some code to set up, but once it is, you
can use it easily:

    Printf ("\n\n The roller outputs for Roll[%d] are:" & 
                "\n Torque-%f\nVelocity-%f\nForce- %f\n",
            (+Roll, +Torque, +Velocity, +Force));

and voila, a Printf that's actually capable of checking the types,
something that C's printf() doesn't do well.  (The object-oriented
features of both Ada 95 and C++ provide ways to handle heterogeneous
data like that more cleanly.)  I used to use a Printf routine like
this, until I found that writing code like Bob's:

 >In Ada, I would write something like this:
 >
 >    use Ada.Text_IO;
 >    ...
 >    New_Line(2);
 >    Put_Line(" The roller outputs for Roll[" & Image(Roll)
 >             & "] are:");
 >    Put_Line(" Torque-" & Image(Torque));
 >    Put_Line(" Velocity-" & Image(Velocity));
 >    Put_Line(" Force-" & Image(Force));

was easier to read and maintain, in my opinion.  This is an option we
Ada programmers have, since we have a string concatenation operator.

Just my contribution to the holy wars . . .

                                -- Adam




  parent reply	other threads:[~1997-06-20  0:00 UTC|newest]

Thread overview: 134+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-05-28  0:00 ada and robots John Bohn
1997-05-29  0:00 ` Stephen Leake
1997-05-29  0:00 ` Michael F Brenner
1997-05-30  0:00 ` John Cook
1997-05-30  0:00   ` Tom Moran
1997-06-01  0:00     ` Dale Stanbrough
1997-06-02  0:00       ` John G. Volan
     [not found]         ` <5mv984$7kn@news.emi.com>
1997-06-03  0:00           ` Martin A. Stembel
1997-06-03  0:00           ` Joe Gwinn
1997-06-04  0:00             ` John G. Volan
1997-06-05  0:00               ` Joe Gwinn
1997-06-14  0:00                 ` Robert Dewar
1997-06-17  0:00                   ` Joe Gwinn
1997-07-03  0:00                     ` Shmuel (Seymour J.) Metz
     [not found]               ` <9706052229.AA29554@jaguar.nmc.ed.ray.com>
1997-06-06  0:00                 ` John G. Volan
1997-06-07  0:00                   ` RC
1997-06-09  0:00                   ` Joe Gwinn
1997-06-04  0:00             ` Pat Rogers
1997-06-05  0:00               ` Joe Gwinn
1997-06-14  0:00                 ` Robert Dewar
1997-06-16  0:00                 ` Ken Garlington
1997-06-16  0:00                   ` Robert Dewar
1997-06-17  0:00                   ` Joe Gwinn
1997-06-28  0:00                     ` Mike Stark
1997-07-03  0:00                       ` Joe Gwinn
1997-06-05  0:00             ` Jon S Anthony
1997-06-05  0:00               ` Joe Gwinn
1997-06-14  0:00                 ` Robert Dewar
1997-06-10  0:00             ` Robert Dewar
1997-06-10  0:00               ` Joe Gwinn
1997-06-11  0:00                 ` Robert Dewar
1997-06-12  0:00                   ` George Haddad
1997-06-16  0:00                   ` Matthew S. Whiting
1997-06-17  0:00                     ` Robert Dewar
1997-06-17  0:00                     ` Robert A Duff
1997-06-18  0:00                       ` Ken Garlington
1997-07-17  0:00                         ` Shmuel (Seymour J.) Metz
1997-06-20  0:00                       ` Robert Dewar
1997-06-20  0:00                       ` Adam Beneschan [this message]
1997-06-17  0:00                     ` Stephen Leake
1997-06-17  0:00                       ` Robert A Duff
1997-06-20  0:00                       ` jim granville
1997-06-21  0:00                         ` Robert Dewar
1997-06-24  0:00                           ` Re(dux): Ada for small machines (was Re: ada and robots) Ken Garlington
1997-06-29  0:00                             ` Robert Dewar
1997-06-29  0:00                         ` ada and robots Matthew Heaney
1997-07-03  0:00                           ` Shmuel (Seymour J.) Metz
1997-07-13  0:00                             ` Robert Dewar
1997-06-17  0:00                     ` Jon S Anthony
1997-06-17  0:00                       ` Matthew S. Whiting
1997-06-18  0:00                         ` Robert A Duff
1997-06-18  0:00                         ` Jon S Anthony
1997-06-22  0:00                           ` John G. Volan
1997-06-18  0:00                         ` Samuel Mize
1997-06-18  0:00                           ` Matthew S. Whiting
1997-06-17  0:00                     ` Samuel Mize
1997-06-18  0:00                       ` Steve O'Neill
1997-06-19  0:00                         ` Anonymous
1997-06-19  0:00                       ` Kenneth W. Sodemann
1997-06-20  0:00                       ` Stephen Leake
1997-06-20  0:00                         ` Robert Dewar
1997-06-04  0:00         ` RC
1997-06-04  0:00           ` John G. Volan
1997-06-04  0:00           ` Larry Kilgallen
1997-06-05  0:00           ` Jon S Anthony
1997-06-02  0:00     ` Nick Roberts
1997-06-04  0:00       ` Jan Galkowski
1997-06-05  0:00         ` Albert K. Lee
1997-06-06  0:00           ` dana
1997-06-07  0:00             ` John G. Volan
1997-06-10  0:00               ` dana
  -- strict thread matches above, loose matches on Subject: below --
1997-06-05  0:00 Marin David Condic, 561.796.8997, M/S 731-93
1997-06-09  0:00 ` Jerry Petrey
1997-06-10  0:00   ` Alan Brain
1997-06-10  0:00     ` Joe Gwinn
1997-06-11  0:00       ` Robert Dewar
1997-06-11  0:00         ` Samuel Mize
1997-06-13  0:00           ` Erik Magnuson
1997-06-17  0:00         ` Joe Gwinn
1997-06-18  0:00           ` Jon S Anthony
1997-06-19  0:00             ` Jonathan Guthrie
1997-06-20  0:00           ` Robert Dewar
1997-06-11  0:00       ` Alan Brain
1997-06-11  0:00         ` Spam Hater
1997-06-11  0:00         ` Joe Gwinn
1997-06-05  0:00 Marin David Condic, 561.796.8997, M/S 731-93
1997-06-09  0:00 Marin David Condic, 561.796.8997, M/S 731-93
1997-06-12  0:00 Marin David Condic, 561.796.8997, M/S 731-93
1997-06-16  0:00 Marin David Condic, 561.796.8997, M/S 731-93
1997-06-17  0:00 ` Joe Gwinn
1997-06-18  0:00   ` Jon S Anthony
1997-06-18  0:00     ` Brian Rogoff
1997-06-20  0:00   ` Robert Dewar
1997-06-23  0:00     ` Richard Kenner
1997-06-23  0:00       ` Robert Dewar
1997-06-23  0:00     ` Geert Bosch
1997-07-02  0:00       ` Robert Dewar
1997-06-25  0:00   ` Will Rose
1997-06-25  0:00   ` Jonathan Guthrie
1997-06-21  0:00 ` Nick Roberts
1997-06-16  0:00 Marin David Condic, 561.796.8997, M/S 731-93
1997-06-19  0:00 Jon S Anthony
1997-06-19  0:00 ` Brian Rogoff
1997-06-20  0:00   ` Jon S Anthony
1997-06-22  0:00   ` John G. Volan
1997-06-25  0:00     ` Richard A. O'Keefe
1997-06-23  0:00   ` Robert Dewar
1997-06-24  0:00     ` Brian Rogoff
1997-06-20  0:00 Ada " Huy Vo
1997-06-23  0:00 ` Jon S Anthony
1997-06-24  0:00 Huy Vo
1997-06-25  0:00 ` Alan Brain
1997-06-25  0:00 ` Dale Stanbrough
1997-06-25  0:00 ` Jon S Anthony
1997-06-25  0:00 ` Wes Groleau
1997-06-26  0:00 ` Ken Garlington
1997-07-01  0:00   ` Tom Moran
1997-06-26  0:00 Huy Vo
1997-06-27  0:00 ` Jon S Anthony
1997-06-27  0:00 ` Richard A. O'Keefe
1997-06-27  0:00 ` Alan Brain
1997-06-27  0:00   ` Stephen Leake
1997-06-27  0:00   ` Wes Groleau
1997-06-27  0:00 ` nma123
1997-06-27  0:00 ` Wes Groleau
     [not found] <867541382.23405@dejanews.com>
1997-06-29  0:00 ` John Howard
1997-06-30  0:00 Huy Vo
1997-07-01  0:00 ` Alan Brain
1997-07-11  0:00   ` Will Rose
1997-07-02  0:00 ` Mattias Sj�sv�rd
1997-07-01  0:00 Huy Vo
1997-07-02  0:00 ` Wes Groleau
1997-07-02  0:00 Huy Vo
1997-07-04  0:00 ` Richard A. O'Keefe
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox