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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,9c9db821cf25f4cf,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1995-03-23 13:56:31 PST Path: nntp.gmd.de!news.rwth-aachen.de!news.rhrz.uni-bonn.de!news.uni-stuttgart.de!rz.uni-karlsruhe.de!xlink.net!howland.reston.ans.net!gatech!paladin.american.edu!auvm!MASS.DNET.HAC.COM!F22HEANEY Comments: Gated by NETNEWS@AUVM.AMERICAN.EDU Newsgroups: comp.lang.ada X-Vms-To: ADA_NEWS Mime-Version: 1.0 Content-Transfer-Encoding: 7BIT Message-ID: <01HOH7KHCU0Y00OPZ6@EDEN1.HAC.COM> Date: Thu, 23 Mar 1995 13:56:31 -0800 Sender: Ada programming language From: Matthew Heaney Subject: Another Way of Emulating printf Date: 1995-03-23T13:56:31-08:00 List-Id: In Magnus Kempe's most recent FAQ, I noticed an item (I think it had been posted by Tucker Taft) that explained how to emulate C's printf function. I've been thinking about it, and propose an alternate (though similar) solution. Tucker's idea was to have the print call take a single argument, and overload "&" to build the argument: Print ("format spec" & A1 & A2); Here is another way: separate the format specification from the arguments, and make the type for the arguments an unconstrained array of a (private) argument type. Each scalar type would have its own generic function to convert to the argument type. I usually name the instantiation "+", to keep the Print call as simple as possible. (I copped this idea from Barnes, who used it to hide allocator calls to make a ragged array.) package Message_Handler is type Argument is private; type Arguments is array (Positive range <>) of Argument; generic type Item is (<>); function From_Discrete (The_Item : Item) return Argument; -- generic -- type Item is digits <>; -- function From_Floating_Point ... -- generic -- type Item is delta <>; -- function From_Fixed_Point ... function "+" (The_Item : String) return Argument; procedure Print ( The_Format : in String; The_Arguments : in Arguments); private Max_Length : constant := 132; -- arbitrary upper bound -- There are probably other ways of implementing type Argument. -- type Argument is record The_Image : String (1..Max_Length); The_Length : Natural := 0; end record; end Message_Handler; with Message_Handler; use Message_Handler; procedure Test_Formatted_Print is function "+" is new From_Discrete (Integer); type Color is (Red, Green, Blue); function "+" is new From_Discrete (Color); I : Integer := 5; C : Color := Blue; begin Print ("format options here", (+I, +"hello", +C) ); end Test_Formatted_Print; Hope this helps! Matt f22heaney@mass.dnet.hac.com