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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fd6dd,c78177ec2e61f4ac X-Google-Attributes: gidfd6dd,public X-Google-Thread: 103376,c78177ec2e61f4ac X-Google-Attributes: gid103376,public From: adam@irvine.com (Adam Beneschan) Subject: Re: ada and robots Date: 1997/06/20 Message-ID: <5oeijc$smf$1@krusty.irvine.com>#1/1 X-Deja-AN: 251415811 References: <33A5D644.37A3@epix.net> Organization: /z/news/newsctl/organization Newsgroups: comp.robotics.misc,comp.lang.ada Date: 1997-06-20T00:00:00+00:00 List-Id: 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