D - extension for printf
- Ben Hinkle (18/18) Mar 09 2004 Here's another proposal for an extensible stream.printf:
- J Anderson (7/26) Mar 09 2004 Good idea!
- Ben Hinkle (8/11) Mar 09 2004 Strings and arrays wouldn't be able to use the %D specifier since they
- Ben Hinkle (5/8) Mar 09 2004 oops - I meant print(..) not printf(...). And maybe the brackets
Here's another proposal for an extensible stream.printf: add the conversion specifier %D (for D object). This specifier will call the object's print method. If object.d also got a print method that accepted options then %D could support formats like %20D etc. Right now stream.printf just calls the underlying printf with the entire format string. To support %D stream.printf would have to go through the format string and call the underlying printf for standard conversions and dispatch any time it sees %D. Similar code would work for printf-like functions like sprintf. If this works out I'd like to change print in object.d to just something like printf("[Object %p]",this); instead of the current printf("Object %p\n",this); I don't recall seeing this proposed before but I could be wrong. thoughts? -Ben
Mar 09 2004
Ben Hinkle wrote:Here's another proposal for an extensible stream.printf: add the conversion specifier %D (for D object). This specifier will call the object's print method. If object.d also got a print method that accepted options then %D could support formats like %20D etc. Right now stream.printf just calls the underlying printf with the entire format string. To support %D stream.printf would have to go through the format string and call the underlying printf for standard conversions and dispatch any time it sees %D. Similar code would work for printf-like functions like sprintf. If this works out I'd like to change print in object.d to just something like printf("[Object %p]",this); instead of the current printf("Object %p\n",this); I don't recall seeing this proposed before but I could be wrong. thoughts? -BenGood idea! And if a D string is being used it would be able to format that properly without using %.*s. It could also do an array of objects (since in D there is a distinction between arrays and pointers). -- -Anderson: http://badmama.com.au/~anderson/
Mar 09 2004
And if a D string is being used it would be able to format that properly without using %.*s. It could also do an array of objects (since in D there is a distinction between arrays and pointers).Strings and arrays wouldn't be able to use the %D specifier since they aren't objects and don't have a "print" method. Using %s for D strings is possible but then it wouldn't work for C strings. So in that case to print a C string you call std.c.printf("%s",...) and to print a D string you call stdout.printf("%s",...). That doesn't look too bad to me, though I haven't thought about the details. Printing arrays of arbitrary types is probably best left to the user. -Ben
Mar 09 2004
If this works out I'd like to change print in object.d to just something like printf("[Object %p]",this); instead of the current printf("Object %p\n",this);oops - I meant print(..) not printf(...). And maybe the brackets [Object %p] isn't that great since it might look like an array. I haven't thought too much about object.print vs object.printf... Walter, why did you choose object.print? I assume it's because printf takes a format string (hence the "f" in printf).
Mar 09 2004