From: Martin <martin.dowie@btopenworld.com>
Subject: 'Image for composite types
Date: Wed, 27 May 2009 15:20:56 -0700 (PDT)
Date: 2009-05-27T15:20:56-07:00 [thread overview]
Message-ID: <f241da28-497c-450a-a804-829eaa1f7608@l28g2000vba.googlegroups.com> (raw)
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
next reply other threads:[~2009-05-27 22:20 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-05-27 22:20 Martin [this message]
2009-05-27 23:23 ` 'Image for composite types Robert A Duff
2009-05-27 23:44 ` Adam Beneschan
2009-05-28 6:46 ` Martin
2009-05-28 9:53 ` Georg Bauhaus
2009-05-28 10:42 ` Martin
2009-05-28 11:31 ` Georg Bauhaus
2009-05-28 16:25 ` Jeffrey R. Carter
2009-05-28 16:47 ` Martin
2009-05-28 3:36 ` Per Sandberg
2009-05-28 5:15 ` tmoran
2009-06-06 9:17 ` Martin
2009-05-28 9:13 ` Dmitry A. Kazakov
2009-05-31 12:17 ` Stephen Leake
2009-06-06 9:17 ` Martin
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox