I have an idea for the type safe printf we've all been dreaming about.
format: a built in keyword
features:
printf-like
type checking not really necessary, the compiler already (usually) knows it,
or cast
returns unknown type? needs to be cast or assigned to the type (like a
string literal is easily a char, char[], wchar[], etc)
always null terminated? (past end of slice) so it's easily used with c
works like a function:
int num = 4;
char[] hi = "foo";
char* bar " baz"; // format treats this like a c null-terminated string
char[] result = format(num, hi, cast(char[])" and", bar);
result is:
"4foo and baz"
works as a function argument: (must be last, like ...)
void dprint(format char[] f) // D's new printf
{
stream.stdout.writeString(f);
}
dprint(cast(char[])"hello ", num, bar); // usage
output:
hello 4 baz
problems:
how does it do the conversion? require the string module?
all the casting is annoying for string literals; maybe it can assume the
result type?
as for integer and float literals that don't specify type, I guess it could
assume the machine's default (int for integer, real for float)?

I have an idea for the type safe printf we've all been dreaming about.

As far as I've seen, the compiler already recognizes to be of type char[].

As far as I know, a string literal is a wchar by default.
If the compiler knows you want it to be a char[] by passing it to a char[]
parameter or assigning to a char[] variable, it automatically becomes one.

I have an idea for the type safe printf we've all been dreaming about.

As far as I've seen, the compiler already recognizes to be of type

As far as I know, a string literal is a wchar by default.
If the compiler knows you want it to be a char[] by passing it to a char[]
parameter or assigning to a char[] variable, it automatically becomes one.

I meant wchar[] and I also made a mistake in my example:
char* bar = " baz"; // format treats this like a c null-terminated string

Good idea.
Unfortunately all that buffering and concatenating takes time.
It would be better to have an interface that can put values directly into a
stream.
Then, you can treat a string as a stream, and voila.
You probably want some kind of temp buffer to prevent constant resizing of
the string as it's built, though.
Anyway this still doesn't account for streaming binary data. Text is only
half the universe. ;)
Sean
"Vathix" <vathix dprogramming.com> wrote in message
news:bektj3$1jd$1 digitaldaemon.com...

I have an idea for the type safe printf we've all been dreaming about.
format: a built in keyword
features:
printf-like
type checking not really necessary, the compiler already (usually) knows

or cast
returns unknown type? needs to be cast or assigned to the type (like a
string literal is easily a char, char[], wchar[], etc)
always null terminated? (past end of slice) so it's easily used with c
works like a function:
int num = 4;
char[] hi = "foo";
char* bar " baz"; // format treats this like a c null-terminated string
char[] result = format(num, hi, cast(char[])" and", bar);
result is:
"4foo and baz"
works as a function argument: (must be last, like ...)
void dprint(format char[] f) // D's new printf
{
stream.stdout.writeString(f);
}
dprint(cast(char[])"hello ", num, bar); // usage
output:
hello 4 baz
problems:
how does it do the conversion? require the string module?
all the casting is annoying for string literals; maybe it can assume the
result type?
as for integer and float literals that don't specify type, I guess it