Re: 'Image for composite types



Have a look on:
http://www.stephe-leake.org/ada/auto_text_io.html
I don know the exact state of it but it may help.
/Per


Martin wrote:
Is it just me that gets fed up with the lack of 'Image on composite
types?

I'm thinking in particular of when I'm testing / debugging and usually
end up writing lots of 'Put_Line' code for each and every record. And
then having to maintain them.

It got me thinking that the language could be expanded to include some
helper attributes for easily composing a function that could return a
String a la 'Image for scalar types.

function record_type_definition'Component_Name (I : Positive)
return String;
function record_type_definition'Component_Image (I : Positive)
return String;
function record_type_definition'Number_Of_Components return
Natural;

Example
=======
type Person is
record
Name : Unbounded_String;
Age : Natural;
Height : Float;
Weight : Float;
end record;
...
procedure Put_Line (P : Person) is
begin
Put_Line (P.Name);
for I in 2 .. Person'Number_Of_Components (P) loop
Put_Line (Person'Component_Name (I) & " => " &
Person'Component_Image (I));
end loop;
end Put_Line;
...
Me : Person := (Name => To_Unbounded_String ("Martin"),
Age => 40,
Height => 1.70,
Weight => 82.5);
begin
Put_Line (Me);
...

Gives output:
Martin
Age => 40
Height => 1.70
Weight => 82.5


Or I could define a generic procedure for any of my record types, e.g.

generic
type Record_Type is private;
procedure Put_Line (R : Record_Type);

procedure Put_Line (R : Record_Type) is
begin
-- Notice: no actual names are used at all...
for R in 1 .. Record_Type'Number_Of_Components loop
Put_Line (Record_Type'Component_Name (R) & " => "
Record_Type'Component_Image (R));
end loop;
end Put_Line;

It might be nice if the language defined a default 'Image for
composites that could be overridden, e.g.

function array_type_definition'Image return String;

Example
=======
type My_Array is array ...;
for array_type_definition'Image use To_String;

function To_String (D : My_Array) return String;


function record_type_definition'Image return String;

Example
=======
type My_Record is record ...;
for My_Record'Image use To_String;

function To_String (R : My_Record) return String;

Anyone think something like this has legs?

Cheers
-- Martin
.



Relevant Pages

  • calling methods in a string
    ... //Prompt user to enter age ... String ageString = JOptionPane.showInputDialog(null, "Enter Age:", ... int age = Integer.parseInt; ... //Prompt user to enter weight ...
    (comp.lang.java.help)
  • Image for composite types
    ... Is it just me that gets fed up with the lack of 'Image on composite ... String a la 'Image for scalar types. ... Weight: Float; ... Person'Number_Of_Components loop ...
    (comp.lang.ada)
  • Help calling methods!
    ... String ageString = JOptionPane.showInputDialog(null, "Enter Age:", ... int age = Integer.parseInt; ... String weightString = JOptionPane.showInputDialog(null, "Enter Weight:", ...
    (comp.lang.java.help)
  • help
    ... String ageString = JOptionPane.showInputDialog(null, "Enter Age:", "Project ... int age = Integer.parseInt; ... String weightString = JOptionPane.showInputDialog(null, "Enter Weight:", ...
    (comp.lang.java.help)
  • extension_pack
    ... It is used to set upper loop -- limits for non-deterministic values thus avoiding the use of access -- types and enabling the functions to be used for synthesizeable code. ... DivisorVal: integer) return std_logic_vector; function "/"(DividendVal: string; DivisorVal: integer) return std_logic_vector; ... for loopVar in 0 to slvVal'length/4-1 loop ... end loop; if then return not resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to else return resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to end if; ...
    (comp.lang.vhdl)