From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, T_FILL_THIS_FORM_SHORT autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!l28g2000vba.googlegroups.com!not-for-mail From: Martin Newsgroups: comp.lang.ada Subject: 'Image for composite types Date: Wed, 27 May 2009 15:20:56 -0700 (PDT) Organization: http://groups.google.com Message-ID: NNTP-Posting-Host: 81.154.95.136 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1243462857 22739 127.0.0.1 (27 May 2009 22:20:57 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 27 May 2009 22:20:57 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: l28g2000vba.googlegroups.com; posting-host=81.154.95.136; posting-account=g4n69woAAACHKbpceNrvOhHWViIbdQ9G User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10, Ant.com Toolbar 1.3,gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:6046 Date: 2009-05-27T15:20:56-07:00 List-Id: 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