DaniFreecs
DaniFreecs

Reputation: 30

Ada: How to represent a java string?

I need some tips/help with one of my home project in Ada. So i need to do a J_String_Package, but i don't really know how to represent my J_string type. The specification asks me to: "represent J_String type as an opaque discriminant record. For the inner representation of the string, use the Standard String type. The discriminant determines the size of the string that is contained in the J_String type." My .ads so far:

package J_String_Pkg is
    type J_String(Size: Positive) is limited private;

   --methods etc    

private
    type J_String(Size: Positive) is record
        --i need some help here!! :)
    end record;
end J_String_Pkg;

Thank you for every help!

Upvotes: 1

Views: 180

Answers (1)

Simon Wright
Simon Wright

Reputation: 25501

You need something like this:

type J_String(Size: Positive) is record
   Contents : String (1 .. Size);
end record;

which corresponds closely to one of the examples in the Ada Reference Manual (ARM 3.7(33)).

One thing to watch out for: your code, without a default for the discriminant, means that once created you won’t be able to change the Size of a J_String. The example from the ARM,

type Buffer(Size : Buffer_Size := 100)  is
   record
      Pos   : Buffer_Size := 0;
      Value : String(1 .. Size);
   end record;

does allow you to change the size of an instance, at the cost of preallocating Buffer_Size characters (at any rate, with GNAT). You do not want to do this with Positive; most computers don’t have 2 gigabytes of RAM to spare!

Upvotes: 3

Related Questions