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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 Path: backlog4.nntp.ams3.giganews.com!backlog4.nntp.ams.giganews.com!border2.nntp.ams.giganews.com!nntp.giganews.com!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!news.stack.nl!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Text_IO, was: Re: Something I don't understand Date: Mon, 17 Feb 2014 21:09:28 +0100 Organization: cbb software GmbH Message-ID: References: <4a3e55f6-9f54-4084-9f37-96efd4b0d349@googlegroups.com> <0b358700-871b-4603-addd-65e07c7d59e5@googlegroups.com> Reply-To: mailbox@dmitry-kazakov.de NNTP-Posting-Host: dRN93LcgZmpMwxQ2TpSF2g.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: 40tude_Dialog/2.0.15.1 X-Notice: Filtered by postfilter v. 0.8.2 Xref: number.nntp.dca.giganews.com comp.lang.ada:184948 Date: 2014-02-17T21:09:28+01:00 List-Id: On Mon, 17 Feb 2014 18:13:40 +0000 (UTC), Simon Clubley wrote: > On 2014-02-17, Niklas Holsti wrote: >> On 14-02-17 14:52 , Simon Clubley wrote: >>> On 2014-02-16, Robert A Duff wrote: >> [snip] >>>> A simplified and type-safe version of C's printf style >>>> (template-based) formatting would be more readable than >>>> concatenating a bunch of strings together to print messages, >>>> and MUCH better than using a series of Put calls to print >>>> a single message. >>>> >>> >>> _Totally_ agree with this. >> >> I disagree, but then I don't understand how Robert would make C's >> template idea type-safe -- might Robert expand on his ideas? > > Oh, I can see a way in which it can be made fully type safe. > > Assume that support for internal to external conversions is implemented > within the type itself as Robert suggests. > > Furthermore, assume, as I suggested, that you need to pass a format > parameter to the type in order for the value to be formatted as required. > > The Ada version of printf would iterate over the combined format string > and pass the fragment of it intended for the formatting of the current > variable to the variable type's Internal_To_External support routine. > > If the passed format fragment was compatible with the data type, the > Internal_To_External routine would process the format and return the > formatted string back to the Ada version of printf so it can be added > into the output buffer. > > If, OTOH, the format fragment was incompatible with the data type, the > Internal_To_External support routine within the type would raise a > exception. Actually it is possible to have it statically type safe with some little compiler support. Not that I like that style of text formatting, I find it extremely boring. Ada's style of independent Put and Get is so much better. Note that even C++ switched to this style, I mean its out<< and in>> operators. Anyway, the method is same as GNAT's Spitbol patterns are treated. Instead of having the format a string, you do it a full-blown type. With terms (built-in literals) defined to format certain types. The full format is composed out of terms using operations like "&". You can make it an aspect if you don't want living objects of the format type, or of a universal type for similar reasons. This has two advantages. Firstly, you require the expression be static and you can check its terms against the types of the values. Secondly the format need not to be parsed at run time. > And voila, C printf templates in a fully Ada type safe manner. Type-safe it is when statically checked. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de