Georg Wrede wrote: > Don wrote: >> Georg Wrede wrote: >>> One thing we sholuld be wary of is overdesign. BigInt is ok, but by >>> the time we try to include a class called >>> CompleteSimulationOfAnF1RaceCar, we're screwed. :-) I see no way to >>> incorporate them into writefln or even plain writeln. Or at least, no >>> *use*. >> >> I think it'd be reasonable to limit things to the options available >> for built-in types. Outside of that, custom formatting functions make >> a lot of sense. The problem is that toString() _looks_ like it >> emulates built-in formatting, but it only does '%s'. So it's really >> beguiling. > > Heh, one thought is, suppose we could have arbitrary format > specifications in writef. Say, if we wrote %&lkjasdf; this string would > be passed to toString. (It would of course be up to the class to > error-check the string, writefln obviously cant (or shouldn't) do it.) > > So, things are doable. But I'm really not looking forward to that kind > of sports. Any elaborate printing or formatting should be handled > outside writefln &co.
Take a look at the formatting library for Plan 9. It’s very much like printf & co., except it exposes enough of its internals to allow for new formats to be added in. See the documentation (for the *nix-ported version) at <http://swtch.com/plan9port/man/man3/print.html> and <http://swtch.com/plan9port/man/man3/fmtinstall.html>. For example: typedef struct { double r, i; } Complex; #pragma varargck type "X" Complex // compiler checks type-safety int Xfmt(Fmt *f) { Complex c; c = va_arg(f−>args, Complex); return fmtprint(f, "(%g,%g)", c.r, c.i); // can use flags here } main(...) { Complex x = (Complex){ 1.5, −2.3 }; fmtinstall('X', Xfmt); print("x = %X\n", x); } With D’s type-safe variadic functions, this model can be made really powerful. —Joel Salomon
