Boost logo

Boost :

From: James Kanze (kanze_at_[hidden])
Date: 2002-02-09 09:37:07


"Brey, Edward D" <EdwardDBrey_at_[hidden]> writes:

|> > From: Darin Adler [mailto:darin_at_[hidden]]

|> > > I've been putting some thought into the syntax of Boost.Format,
|> > > and have come to see that the best syntax going forward is one
|> > > that does not try to retain compatibility with printf as a high
|> > > priority.

|> > I don't agree with this premise.

|> Even though that statement was at the top of my email, it isn't
|> really a premise. It's really a conclusion. Printf compatibility
|> and elegant syntax are mutually exclusive, in light of what can be
|> done with more modern syntaxes. Therefore, ... (the above).

|> > While I think it might be useful to pursue a
|> > string-formatting library that
|> > doesn't have printf compatibility, I do think that a library
|> > that provides
|> > printf compatibility is quite useful, and is nearly
|> > accomplished with the
|> > proposed format library. That is the reason I propose
|> > removing the "%1" from
|> > the library and retaining only the "%1$s" form. I don't think that the
|> > precedence problem with operator % is severe.

|> I agree with the desire for printf compatibility. But trying to
|> find a nice syntax that allows it just isn't easy. For example,
|> using the "%1$s" use mentioned above, what happens if the value
|> substituted is a string that the context string will make plural.
|> Its not pretty: format("%1$s %2$ss") [3] ["banana"].

But it doesn't occur in practice. You can't just add an s to make a
word plural. Normally, this would be something along the lines of:

    format( gettext( "%1$s %2$s" ) )
        .with( count )
        .with( gettext( fruitNames[ count == 1 ] ) )

|> What harm is there in completely separating the printf syntax into a
|> different class? Is there really any desire to use (pseudo-)printf
|> syntax and a modern syntax within the same format object?

I've yet to see a suggestion which effectively separates formatting and
parameter ordering. I'd like to. I agree that they are two different
issues completely. But I don't see how to separate them effectively.

|> > For what it's worth, where printf compatibility is not an issue in
|> > my programs, I use a formatting system with a syntax like this:

|> > "There's no space left on the disk named <disk>."

|> You're in luck. C++ has this!
|> cout << "There's no space left on the disk named " << disk << ".";

|> <big grin>
|> Of course, I agree with you. Something like this could be quite
|> nice to have.

I can't resist citing CAy Horstmann's home page here:

    The March of Progress

    C
          printf("%10.2f", x);
    C++
          cout << setw(10) << setprecision(2) << showpoint << x;
    Java

          java.text.NumberFormat formatter
             = java.text.NumberFormat.getNumberInstance();
          formatter.setMinimumFractionDigits(2);
          formatter.setMaximumFractionDigits(2);
          String s = formatter.format(x);
          for (int i = s.length(); i < 10; i++)
             System.out.print(' ');
          System.out.print(s);

Although he forgot the fixed in C++, not to mention putting the stream
back into it's original state.

Viewed this way, maybe the printf syntax isn't so bad afterall.

-- 
James Kanze                                mailto:kanze_at_[hidden]
Conseils en informatique orientée objet/
                    Beratung in objektorientierter Datenverarbeitung
Ziegelhüttenweg 17a, 60598 Frankfurt, Germany Tel. +49(0)179 2607481

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk