lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 8/13/2013 4:35 AM, Jon Akhtar wrote:
I have seen OO's answer to printf. That was funny. both in C++ and in Java early on. If you don't like printf or its ilk - which encode their state machines in a readable format - what do you like? I think you haven't yet thought it through from the viewpoint of the programmer who is not a language nerd.

I tend to agree that printf gives you a nice readable format compared to the other options.

The STL answer to printf sucks. It's way too verbose and it's too hard to understand what the resulting string will look like by glancing at it. It also doesn't allow for positional parameters.

The Boost answer to printf is much better, but still has issues. (it's similar to Python's %: "%s: %d" % "string arg" % 12). It is typesafe, but its failure mode is "crash" (throw exception, sure, but unless you wrap every format with a try/catch, you're likely short-circuiting functionality).

MY answer was the string format function in the Playground library (now defunct). It acts like printf, but with templated variable arguments (up to 14 arguments, IIRC, though C++11 would fix that) that are type safe, and they're also set up so you can define specific behaviors for each object you pass in based on the format types, with the addition of positional parameters ("%1%" means put the first parameter here; this is used extensively when creating translations with multiple substitutions, because word order can change from one language to another).

If you pass in an object without a template definition, it's a compile error. When you define the formatting action for an object, it requires that you handle string, int, and double format cases; if you ask for %d or %f for a string, it might return the length of the string, but it won't crash. If you ask for a string (%s) and give it a number, it would hand you a reasonable format for the number type in question. If you accidentally pass in a pointer instead of an object, it will print the pointer in a reasonable way (a %s will print it as a %x, for instance, though %f might just print 0 and log an error -- no crashing necessary). If you give it a positional parameter, it picks the right one; if one doesn't exist, it could substitute in a string to that effect, but it's typesafe and doesn't need to be undefined or throw an error.

As far as I am concerned, that was by far the best answer I've ever seen for string formatting in C++, and it's what I miss the most about that library (it's impossible to download and use legally any more, though at one time it was free). People familiar with printf could act as if this were a magical printf that couldn't crash, was type safe, and always did something reasonable with whatever you pass (or it wouldn't compile). Maybe someday I'll reimplement it in an open source/free library (complete with Lua bindings, of course). For now I'm just doing enough more of my code in Lua that I haven't bothered.

Tim