lua-users home
lua-l archive

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

On Tue, Aug 13, 2013 at 02:18:43AM -0400, Jay Carlson wrote:
> On Aug 8, 2013, at 9:37 AM, steve donovan wrote:
> > On Thu, Aug 8, 2013 at 3:20 PM, Lorenzo Donati <> wrote:
> >> fmt.Println(time.Now().Format("2006-01-02 03:04"))
> > It is very .. eccentric. Rob Pike is very proud of this idea ;)
> It is a cute idea, but just like printf, it's a symptom of a lack of expressive power at compile-time.
> Famously, the Pascal runtime cannot be implemented in terms of itself. C is much better, but originally had no standard mechanism for functions with a variable number of arguments either; there was no way of writing your own printf. 
> An informal mechanism and then a standard were created, and they're designed to barely handle printf/scanf, and forwarding of those varargs. 
> Most C compilers these days have special mechanisms hooked up to the
> printf/scanf functions to warn of mismatches in type between the format
> string and the arguments. So there's a special case again: you can't write
> your own replacements with the same functionality, especially if you are
> in the -Werror camp, where warnings are treated as errors.

You can get close. Using C99's variable argument macros and C11's _Restrict,
you can translate at compile time each argument to a pair of arguments--type
and value. This type information can then be used at run time by the printf

But that's rather ugly. Personally I think C's printf is an elegant
interface to a thorny problem for statically typed, compiled languages that
lack a dynamic execution capability at compile time.

The string template itself is not only a solution to a problem--dynamic
string composition--but a feature all by itself. That's why the concept is
often copied by so many other languages; specifically, concise format
specifications that are filled in by values specified elsewhere, and the use
of tiny domain-specific languages.