2

I'm a bit of a beginner when it comes to Ada, and I'm trying to declare and use an array of strings of different lengths.

Using Ada'83 I can declare an array of variable length string constants as follows (example taken from the Ada FAQ)

type table is access String;

 TESTS : constant array (Positive range 1..3) of table
         := ( 1 => new String'("One"),
              2 => new String'("Two"),
              3 => new String'("Three")
            );

However much to my frustration even though the result appears to be an array of character arrays they don't behave as strings. When I try to compile the following code I get an error message 'Inconsistency detected during overload resolution [LRM 8.7]'

for COUNT in TESTS'Range loop
   Put(TESTS(COUNT));
   New_Line;
end loop;

However, I can print out the content of each of the 'strings' using the following code.

for COUNT in TEST'Range loop
   for COUNTER in TEST(COUNT)'Range loop
      Put(TEST(COUNT)(COUNTER));
   end loop;
   New_Line;
end loop;

Unfortunately I want to use the values to test some code that takes a string as a parameter, so this doesn't really help...

Is there a way to be to iterate over an array of string constants of varying length in Ada'83, or to convert the character arrays into strings of varying length.

Thanks

No, this isn't homework, and yes, I know I'm using an ancient compiler!

7
  • 1
    Is that the Americans with Disabilities Act or Aged and Disabled Advisors? Ada is a woman's name, not an acronym. Commented Apr 5, 2021 at 5:11
  • 1
    Note that Ada Home is very out of date and may contain incorrect information. Commented Apr 5, 2021 at 5:15
  • 1
    Why are you using Ada 83? It is a better language than most of the commonly used languages today, but it has some warts, most of which were addressed in later versions of the language. A free Ada-12 compiler is probably available for the computer you're using. Commented Apr 5, 2021 at 5:39
  • Changed ADA to Ada (it never occurred to me that it might be different from FORTRAN COBOL BASIC etc!) 'A free Ada-12 compiler is probably available for the computer you're using' - I doubt it I'm using a MicroXAX (3100)!
    – Mike T.
    Commented Apr 5, 2021 at 11:18
  • 1
    The DEC Ada-95 compiler for VAX was actually GNAT, so that's probably available somewhere. Commented Apr 6, 2021 at 10:42

3 Answers 3

6

Test is undefined; I'll presume you mean Tests.

Table is not a string type; it is an access type. To reference the value that an access value designates, one uses .all:

Tests (Tests'First).all

is a string. Ada contains some shortcuts for access-to-array types to make them easier to use, allowing .all to be left off before attributes and indexing, which is why Tests (Count)'Range and Tests (Count) (Counter) work. To reference the whole value, though, .all is required:

Text_IO.Put_Line (Item => Tests (Counter).all);

However, a better approach would be to define a variable-length string abstraction and use that instead of an access type.

2
  • Thank you that works a treat - however, how would I go about defining 'a variable-length string abstraction' to do the same job?
    – Mike T.
    Commented Apr 5, 2021 at 11:32
  • That depends on how universal you want them to be, and whether you want them to be bounded or unbounded. In general, you define a package with a [limited] private type and conversions to/from String, and any other useful operations you want (comparison, concatenation, slices). One problem with Unbounded abstractions in Ada 83 is that it doesn't have Finalization, so users have to manually call an operation to avoid memory leaks. Commented Apr 6, 2021 at 10:53
3

Thank you that works a treat - however, how would I go about defining 'a variable-length string abstraction' to do the same job?

Use private-types + access-types, perhaps like the following:

Package String_Abstraction is
    
    Type DString is private;
    Function "+"( Right : DString ) return  String;
    Function "+"( Right :  String ) return DString;
    Function "&"( Left, Right : String ) return DString;
    --...
Private
    Type Data(<>);
    Type DString is access Data;
End String_Abstraction;

with implementation of:

Package Body String_Abstraction is
    Type Data( Length : Natural ) is record
        Text : String(1..Length) := (others => ASCII.NUL);
    end record;
    
    Function "+"( Right : String ) return DString is
    Begin
        Return New Data'( Text => Right, Length => Right'Length );
    End "+";
    
    Function "&"( Left, Right : String ) return DString is
    Begin
        Return +(Left & Right);
    End "&";
    
    Function "+"( Right : DString ) return  String is
    Begin
        Return Right.Text;
    End "+";
    
    --...
End String_Abstraction;

Which could be used as follows:

Table : Constant Array(Positive range <>) of String_Abstraction.DString:=
  ( String_Abstraction."+"( "This" ),
    String_Abstraction."+"( "EXAMPLE" ),
    String_Abstraction."+"( "list" ),
    String_Abstraction."+"( "exists." )
  );

and

Print_Table:
For Index in Table'Range Loop
    Declare
        Use String_Abstraction;
        Item : DString renames Table(Index);
    Begin
        Ada.Text_IO.Put_Line( +Item );
    End;
End loop Print_Table;

If you had a use prior to the declaration of table, you could have:

Use String_Abstraction;
Table : Constant Array(Positive range <>) of String_Abstraction.DString:=
  ( +"This",
    +"EXAMPLE",
    +"list",
    +"exists."
  );

It's certainly not complete, but that gives you the basic idea of how to do it.

4
  • It's also not Ada 83 and leaks memory. Commented Apr 6, 2021 at 10:49
  • How is it not Ada83? (I only have GNAT2019 on this computer.) -- As to leaking memory, it only never deallocates, which is fine for a global-constant table or a small example showing the 'thrust' of how to use private-types and accesses.
    – Shark8
    Commented Apr 6, 2021 at 13:41
  • It's not Ada 83 because it uses unknown discriminants ["(<>)"], which did not exist in Ada 83. Commented Apr 10, 2021 at 5:45
  • Huh. I thought that one went all the way back to 83.
    – Shark8
    Commented Apr 10, 2021 at 17:43
0

GNAT Studio Community Edition is free to use for freelance use and hobby going coders. It comes pre-installed with a decent Ada 2012 compiler. I had to also install a small add on called Ada GDE to get the code to run.

If your looking for an up to date book to study from, I highly recommend Ada 2012 by john Barnes. It's a bit of a beast at over 900 pages long, but he goes into good detail on concepts and provides plenty of sample code to help. He also wrote one for Ada 95 I believe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.