comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Text_IO, was: Re: Something I don't understand
Date: Wed, 19 Feb 2014 16:46:45 -0500
Date: 2014-02-19T16:46:45-05:00	[thread overview]
Message-ID: <wccppmi4vy2.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: bmetfvFp5prU1@mid.individual.net

Niklas Holsti <niklas.holsti@tidorum.invalid> 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


  parent reply	other threads:[~2014-02-19 21:46 UTC|newest]

Thread overview: 77+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-13 23:57 Something I don't understand Laurent
2014-02-14  0:18 ` adambeneschan
2014-02-14  7:05   ` Charles H. Sampson
2014-02-15 15:27   ` Laurent
2014-02-15 19:10     ` Laurent
2014-02-15 20:05       ` Niklas Holsti
2014-02-15 21:16         ` Laurent
2014-02-15 21:40       ` Jeffrey Carter
2014-02-16  1:39       ` Robert A Duff
2014-02-16  9:08         ` Text_IO, was: " Simon Clubley
2014-02-16  9:43           ` Dmitry A. Kazakov
2014-02-16 16:57             ` Dennis Lee Bieber
2014-02-16 16:17           ` Robert A Duff
2014-02-17 12:52             ` Simon Clubley
2014-02-17 15:32               ` G.B.
2014-02-17 15:35                 ` G.B.
2014-02-17 17:34                 ` Mike H
2014-02-17 16:59               ` Niklas Holsti
2014-02-17 17:17                 ` Dmitry A. Kazakov
2014-02-17 17:42                   ` Niklas Holsti
2014-02-17 19:55                     ` Dmitry A. Kazakov
2014-02-18  7:14                       ` Niklas Holsti
2014-02-18  8:40                         ` Dmitry A. Kazakov
2014-02-18  9:00                           ` Niklas Holsti
2014-02-18  9:31                             ` Dmitry A. Kazakov
2014-02-19  8:36                               ` Niklas Holsti
2014-02-19  9:40                                 ` Dmitry A. Kazakov
2014-02-19 13:20                                   ` Niklas Holsti
2014-02-19 14:13                                     ` Dmitry A. Kazakov
2014-02-19 15:37                                       ` Georg Bauhaus
2014-02-19 16:32                                         ` Laurent
2014-02-19 17:46                                           ` Simon Clubley
2014-02-20  2:39                                         ` Dennis Lee Bieber
2014-02-20 11:44                                           ` G.B.
2014-02-19 21:45                                       ` Niklas Holsti
2014-02-20  9:52                                         ` Dmitry A. Kazakov
2014-02-20 18:19                                           ` Niklas Holsti
2014-02-19 15:06                                     ` Robert A Duff
2014-02-19 17:03                                       ` Niklas Holsti
2014-02-19 22:30                                         ` Robert A Duff
2014-02-17 18:13                 ` Simon Clubley
2014-02-17 20:09                   ` Dmitry A. Kazakov
2014-02-18  7:50                     ` Georg Bauhaus
2014-02-18  8:28                       ` Dmitry A. Kazakov
2014-02-17 20:22                   ` Niklas Holsti
2014-02-18  0:50                     ` Simon Clubley
2014-02-18  6:56                       ` Niklas Holsti
2014-02-18  8:04                         ` Georg Bauhaus
2014-02-19 22:01                     ` Robert A Duff
2014-02-20  8:25                       ` Dmitry A. Kazakov
2014-02-20 15:54                         ` Robert A Duff
2014-02-20 17:54                           ` Dmitry A. Kazakov
2014-02-20 20:45                       ` Niklas Holsti
2014-02-19 21:52                   ` Robert A Duff
2014-02-20  0:50                     ` Simon Clubley
2014-02-19 21:46                 ` Robert A Duff [this message]
2014-02-20  0:09                   ` Jeffrey Carter
2014-02-20  1:09                     ` Simon Clubley
2014-02-20  7:06                       ` Niklas Holsti
2014-02-20 13:05                         ` Simon Clubley
2014-02-20 11:51                       ` G.B.
2014-02-20 12:53                         ` Simon Clubley
2014-02-21 11:50                       ` Brian Drummond
2014-02-23 21:37                         ` AdaMagica
2014-02-23 23:23                           ` Bill Findlay
2014-02-24  4:29                           ` AdaMagica
2014-02-24 12:22                           ` Brian Drummond
2014-02-24 19:03                             ` AdaMagica
2014-02-20 20:02                   ` Niklas Holsti
2014-02-19 21:15               ` Robert A Duff
2014-02-19 22:01                 ` Simon Clubley
2014-02-16 14:50         ` Mike H
2014-02-17 16:09         ` Laurent
2014-02-17 17:42           ` Mike H
2014-02-18  1:05             ` Dennis Lee Bieber
2014-02-17 22:31           ` Jeffrey Carter
2014-02-19 12:51             ` Laurent
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox