The March of Progress (in programming language syntax :)
alan.dipert.org
alan.dipert.org
1956: Fortran I:
PRINT 1, X
1 FORMAT (F10.2)
Fortran was able to use the same format specification for more than one print statement, that's why the format statement is referenced by number. It was even able to do repeats, for example this code from 1956 PRINT 2, X, Y, A, B, C, D, N
2 FORMAT ( 3 ( F10.3, F10.4 ), I4 )
is an equivalent of the following C: printf( "%10.3f%10.4f%10.3f%10.4f%10.3f%10.4f%4d",
x, y, a, b, c, d, n );
Moreover, C interprets the format specification at runtime, in FORTRAN the compiler uses it during the compilation.Fortran was invented by John Backus and there was already a finished compiler in 1956:
"Programmer's Reference Manual Fortran Automatic Coding System For IBM 704"
http://www.fh-jena.de/~kleine/history/languages/FortranAutom...
I/O in the first Fortran, I believe including the FN.M syntax, was implemented by Roy Nutt, also one obvious genius:
http://www.smartcomputing.com/editorial/dictionary/detail.as...
printf( format, x, y, a, b, c, d, n );
You can get similar behavior from C, reusing the format specification? No idea if this was true in the K&R days though.
Still whoever uses some printf variant today, misses some aspects of what was working in 1956.
Those GCC checks you mentioned are handy and would have avoided a bug in production code a colleague encountered a few years ago, but I imagine they are more recent than C in 1982, and certainly more recent than Fortran in 1956
#define formata "%10.3f%10.4f"
#define formatb(z) z z z "%4d\n"
printf( formatb(formata), x, y, a, b, c, d, n );
The example you've given is thought provoking!
OCaml and Rust both have compile-time format string checking and code generation as well.
MACLISP (1980):
(format t "Week ~R of the ~:R month of the year ~:@R~%" 15 102 1996)
--> Week fifteen of the one hundred second month of the year MDCCCCLXXXXVI
C/C++/Java/Python/Ruby:
...uh...
COMMON LISP:
(format t "Week ~R of the ~:R month of the year ~:@R~%" 15 102 1996)
--> Week fifteen of the one hundred second month of the year MDCCCCLXXXXVI
There are powerful print functions and then there are powerful print functions.Of course, like all irony the glass is only half funny.
printf "%10.2f"
-- Haskell, 1990.I personnaly don't like the printf/scanf style, which unfortunately often prevails in newer languages. I definitely prefer the string interpolation method, or just plain string concatenation.
You're mixing things up; the point of printf is to format data, of which "%s / %s / %s" is a subset. Constructs like "0x%04x" or "%12s" really are about formatting.
For my part, I've never personally liked the use of "+", in particular, to denote concatenation, for the (admittedly silly) reason that string concatenation is not commutative. If nothing else, it's a bit of a slippery slope to even more dubious operator overloads, though not nearly so much as C++'s bloody I/O "operators"!
This takes all its importance with duck-typing, as syntactic sugar of binary operators will be translated to a method call, e.g in Ruby a + b becomes a.+(b) [0]. while in Python it's a.__add__(b) [1]. The result might be commutative, but the call and evaluation order certainly is not.
[0] http://ruby-doc.org/core-1.9.3/Fixnum.html#method-i-2B [1] http://docs.python.org/reference/datamodel.html#object.__add...
Using Scala and co only brings you a part of the way, especially if you have to interact with Javaesque APIs like in Android, where you have dozens of different abstract classes (or sometimes interfaces) begging for anonymous inner classes.
Now, most programmers only speculate what a compiler does. They are so focused on language details.
Few want to write compilers. Many want to write yet another new language.
As a Java programmer, I agree that NumberFormat looks ridiculous there. It's a lot of work for a single number to string conversion.
Luckily, since Java 5 there is a printf function:
System.out.printf(...);You just have to type hello world in HTML. Beat that.
Progress™!