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=unavailable autolearn_force=no version=3.4.4 Path: border1.nntp.dca3.giganews.com!backlog4.nntp.dca3.giganews.com!border4.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!news.snarked.org!nntp.club.cc.cmu.edu!micro-heart-of-gold.mit.edu!newsswitch.lcs.mit.edu!nntp.TheWorld.com!.POSTED!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Text_IO, was: Re: Something I don't understand Date: Wed, 19 Feb 2014 16:46:45 -0500 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <4a3e55f6-9f54-4084-9f37-96efd4b0d349@googlegroups.com> <0b358700-871b-4603-addd-65e07c7d59e5@googlegroups.com> NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls7.std.com 1392846409 870 192.74.137.71 (19 Feb 2014 21:46:49 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Wed, 19 Feb 2014 21:46:49 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:p13SNEwBgk8tb5JFh1taWynBQoQ= X-Original-Bytes: 5484 Xref: number.nntp.dca.giganews.com comp.lang.ada:185006 Date: 2014-02-19T16:46:45-05:00 List-Id: Niklas Holsti writes: > I disagree, but then I don't understand how Robert would make C's > template idea type-safe -- might Robert expand on his ideas? Most people call me "Bob". Yeah, I know my "From:" line say "Robert". ;-) My answer depends on if (or how much) I'm allowed to change Ada's type system. I've done this in pure standard Ada. Something like: type Template is new String; procedure Put(T: Template; X1, X2, X3, X4, X5, X6, X7, X8: String := ""); Put("There were \1 warnings and \2 errors.\n", Image(Warning_Count), Image(Error_Count)); prints the template as is to standard output, except it replaces \1 with X1, and \2 with X2, and the "\n" is new-line. You can have multi-line templates. "\\" represents "\". Note my use of X1...X8 to simulate C's variable-length parameter lists. This is a kludge. Note that Template is a different type from String. This prevents a bug that can happen in C, where you say printf(blah), and blah is some data read off the internet. That can be a security hole! The user is responsible for writing suitable Image functions for their data types, and they can take whatever formatting parameters you like. This seems much more readable than C's way of encoding the field widths and whatnot in the template. For localization/internationalization, you can have a table mapping "There were \1 warnings and \2 errors.\n" to the corresponding template in (say) French. Different languages have different word orders, so you might have: "... \2 ... \1 ... \n" to reverse the order of insertion. This is all 100% type safe. Most of the checking is static. It checks at run time that the number of \1, \2, \3, ... escapes matches the number of non-empty Xn parameters passed. Now, if you let me change Ada, I'd allow user-defined literals. Any type derived from the Has_Literals interface allows literal syntax, and it overrides the Literal_Value function to convert the sequence of characters to that type. Template would no longer need to be derived from String; it could be a private extension of Has_Literals, and the Literal_Value function could "precompile" the template into some convenient/efficient internal form. The Literal_Value call is evaluated at compile time, so the above-mentioned run-time check can now be static. Change the types of the Xn parameters to Has_Image'Class, so you can pass Warning_Count, and it automatically dispatches to Image(Warning_Count). If you want to use extra formatting options you'd call your own Image function explicitly. Integer types are derived from Has_Image, and the Image function is not an attribute, and (most importantly of all! ;-)) it doesn't insert an annoying extra blank. I'd also allow variable-length argument lists and/or arrays of strings. > I think that the present method of concatenating strings or using > several Puts is good; I think Put (at least to standard output/error) should be task safe. That is, it should be atomic with respect to other tasks doing Put. Puts from different tasks would be interspersed, but you wouldn't get character-by-character interspersal, and you certainly wouldn't get the Ada rule ("erroneous and therefore unpredictable behavior"). That rules out the "series of Puts" method. You need to build up your whole message (possibly multi-line) and then Put it in one fell swoop. The concatenating strings method just looks ugly to me -- I can't easily see what the message is going to look like. With a template, I see the whole message, with marks for where variable data is inserted. >...what is needed is to extend or replace the 'Image > attribute with similar value-to-string functions which are more > controllable, flexible, and work also for composite types. Perhaps > something analogous to the stream attributes, but with the ability to > control the output format at each invocation, which is not possible with > the stream attributes. It is odd that in Ada, 'Image doesn't support the same formatting control as Text_IO. - Bob