How to inherit string handling?

Assume you define a “new” fixed string type:

type s_type is new String;

s_type inherits primitive operations from the type sting.
However, is it any simple way where s_type also “inherits” operations from Ada.Strings.Fixed ?

reinert

Since the operations are in a different package from where String is declared (Standard), I don’t think the operations in Fixed count as primitive operations for String types. I think in order to inherit an operation, it has to be a primitive operation.

Yes, but is there a possible (almost-)workaround?

Do you mean, a simple way other than subtype S_Type is String; ?

The idea was to be able to do something like this:

declare
S : S_type := “This is a test”;
begin
S.New_Operation; – with this “OOP notation”
end;

(and at the same time easily/directly inherit the standard (library) operations for String).

Just experimenting with a string/command parser for my program:-)

You could try turning on GNAT extensions if using GNAT. They have Object.Operation notation for all types (which isn’t a language feature yet), but I don’t know how it works with operations declared in external packages.

Otherwise you will probably have to do some work and make your own tagged type to wrap around unbounded_string and then put in wrapper operations for all of the functions/procedures you want. Something like:

with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Strings.Maps;      use Ada.Strings;

package OO_String is

   type Tagged_String is tagged record
      Implementation : Unbounded_String;
   end record;

   -- Quick conversion functions
   function "+"(Source : String) return Tagged_String is
      (Implementation => To_Unbounded_String(Source));
   function "+"(Source : Tagged_String ) return String is
      (To_String(Source.Implementation););

   function Index (Source   : in Tagged_String;
                   Pattern  : in String;
                   Going    : in Direction := Forward;
                   Mapping  : in Maps.Character_Mapping
                                := Maps.Identity)
                   return Natural
   is (Index(Source.Implementation, Pattern, Going, Mapping));

end OO_String;

Then you can do things like:

   My_String : Tagged_String;
   Index : Natural;
begin
   Index := My_String.Index(".");

Unless this is a way of learning, consider re-using one of the numerous libraries out there that attempt to wrap all those strings operations. They have been mentioned several times in this forum, and their various authors are in general participating here.

(at the time, I contributed GNATCOLL.Strings as such as an attempt – the AdaCore people seem to have now come up with a different package in VSS, I think there is also something similar in pragmARC, and maybe Vadim Godunko also has another version). Seems like we have all had a slightly different goal in mind, and different approach to the code, so take the time to examine the packages to see which one would better match your needs.

2 Likes