preprocessing printf/regex strings (like ocaml)

Robert Ennals Robert.Ennals@cl.cam.ac.uk
Tue, 14 May 2002 15:45:36 +0100


> Martin Norb=E4ck <d95mback@dtek.chalmers.se> wrote:
> > I agree that i18n needs positional arguments.
> > What's wrong with simply doing like this:
> > =

> > printf "I have %. %. %.."    ["trained", show 1, "Jedi"]
> > printf "%2. %3. %1. I have." ["trained", show 1, "Jedi"]
> =

> Nothing is exceptionally wrong with it, except it's not
> as flexible. Since everything is show'n, how would you
> handle things like "%5.2f" or "%*d"? In Brian Huffman's
> version it's almost trivial to add. I know I can use =

> formatDouble and whatnot, but the code looks cluttered
> this way. "C" printf has many pitfalls, but I like its
> terseness.

Just thought I would jump in and say that, unlike (it seems) everyone els=
e, I =

hate printf in C. It is a horrible horrible inextensible hack of a functi=
on =

that I find extremely awkward to use.

In the C version, it is completely hardcoded and inextensible. Even in th=
e =

version presented on this list, one can't add new ways to format an exist=
ing =

datatype.

I personally much prefer the syntax currently used in Haskell, which is a=
lso =

essentially what is used in most other recent languages, including Java, =
C++, =

and (god help me) Perl.

In the example given, I could write:

"I have " ++ action ++ " " ++ number ++ " " ++ whatas
where
    action =3D "trained"
    number =3D show 1
    whatas =3D "Jedi"

Which is IMHO rather more readable than a load of weird control codes hid=
den =

in a text string that one then has to match against a list.

+ If I want to use a weird formatting approach, I just write my own funct=
ion, =

and use it instead of "show". No need to faff around extending someone el=
se's =

printf.

[end rant]


-Rob