Seeing recent discussions like "formatted output trial balloon", "stream
operator suggestions", "printf and read/write a proposal", etc., I'm
encouraged to post my proposal for type-safe varargs again - since I
still think it's a Good Idea.
Here's an example of the syntax for a type-safe varadic function:
int print(PrintParam args...)
What this says is that the function "print" takes a variable number of
arguments, and each is of type "PrintParam", and they have been
collected into a dynamic array "PrintParam args[]".
So imagine we have:
class PrintParam
{
this(char[] arg) { /* build a PrintParam from a string */ }
this(int arg) { /* build a PrintParam from an int */ }
/* etc, ad infinitum */
...then construction of this array is done automatically by the compiler
when the user invokes the function "print", something like this:
int d = 2;
print("The value of D is ", d, "\n");
(Oh, by the way: Notice that this is the Perfect Syntax for a print
function - just arguments, separated by commas.)
...then the compiler produces code equivalent to this:
PrintParam _t[];
_t[0] = new PrintParam("The value of D is ");
_t[1] = new PrintParam(d);
_t[2] = new PrintParam("\n");
print(_t);
The compiler coerces each argument into the proper type, in this case
PrintParam. It does so by looking for a constructor which can be used
to build a PrintParam (if the argument isn't already compatible with
PrintParam, that is).
You can imagine each PrintParam constructor collecting it's arg into
something variant-like, or performing the conversion to string, or
something else.
You can also imagine formatters derived from PrintParam:
class WidthParam : PrintParam
{ this(int w) { /* whatever */ }
}
WidthParam width(int w)
{
return new WidthParam(w);
}
print(width(16), var, "\n");
The "width" function returns a WidthParam, which is compatible with
PrintParam because it's a derived class, and so is already an acceptable
parameter; nothing need be automatically constructed. Now, while
"print" is walking it's argument array, it can see if any is a
WidthParam, and so apply the modifier to the next output operation.
To make an object printable, add a method that returns a PrintParam
constructed from it:
class MyWeirdObject
{
PrintParam toPrint() { /* ... */ }
...
And then, call it:
print(myWeirdo.toPrint());
Sadly, I've not been able to figure out how to make your own objects
implicitly printable (as in "print(myWeirdo)"), unless they're derived
from PrintParam, without adding something else to the language, either
multiple inheritance (PrintParam can't be an interface, because it must
be directly constructible) or type-cast operator overloads (defining
method "operator PrintParam()" in your class).

Seeing recent discussions like "formatted output trial balloon", "stream
operator suggestions", "printf and read/write a proposal", etc., I'm
encouraged to post my proposal for type-safe varargs again - since I
still think it's a Good Idea.
Here's an example of the syntax for a type-safe varadic function:
int print(PrintParam args...)
What this says is that the function "print" takes a variable number of
arguments, and each is of type "PrintParam", and they have been
collected into a dynamic array "PrintParam args[]".
So imagine we have:
class PrintParam
{
this(char[] arg) { /* build a PrintParam from a string */ }
this(int arg) { /* build a PrintParam from an int */ }
/* etc, ad infinitum */
...then construction of this array is done automatically by the compiler
when the user invokes the function "print", something like this:
int d = 2;
print("The value of D is ", d, "\n");
(Oh, by the way: Notice that this is the Perfect Syntax for a print
function - just arguments, separated by commas.)
...then the compiler produces code equivalent to this:
PrintParam _t[];
_t[0] = new PrintParam("The value of D is ");
_t[1] = new PrintParam(d);
_t[2] = new PrintParam("\n");
print(_t);
The compiler coerces each argument into the proper type, in this case
PrintParam. It does so by looking for a constructor which can be used
to build a PrintParam (if the argument isn't already compatible with
PrintParam, that is).
You can imagine each PrintParam constructor collecting it's arg into
something variant-like, or performing the conversion to string, or
something else.
You can also imagine formatters derived from PrintParam:
class WidthParam : PrintParam
{ this(int w) { /* whatever */ }
}
WidthParam width(int w)
{
return new WidthParam(w);
}
print(width(16), var, "\n");
The "width" function returns a WidthParam, which is compatible with
PrintParam because it's a derived class, and so is already an acceptable
parameter; nothing need be automatically constructed. Now, while
"print" is walking it's argument array, it can see if any is a
WidthParam, and so apply the modifier to the next output operation.
To make an object printable, add a method that returns a PrintParam
constructed from it:
class MyWeirdObject
{
PrintParam toPrint() { /* ... */ }
...
And then, call it:
print(myWeirdo.toPrint());
Sadly, I've not been able to figure out how to make your own objects
implicitly printable (as in "print(myWeirdo)"), unless they're derived
from PrintParam, without adding something else to the language, either
multiple inheritance (PrintParam can't be an interface, because it must
be directly constructible) or type-cast operator overloads (defining
method "operator PrintParam()" in your class).

Why not adding some intelligence to the compiler?
int print(..., Printable[] args, DefaultPrintable)
Here Printable is an interface, and the compiler does this:
for each parameter Pi
{
if Pi is a Printable
then args[i] = Pi
else args[i] = new DefaultPrintable(Pi)
}
So the example:
MyClass d = something(); // MyClass is a Printable
print("The value is: ", d, endl);
becomes:
Printable _t[];
_t[0] = new DefaultPrintable("The value is: "); // char[] is not a Printable
_t[1] = d; // d is a Printable
_t[2] = endl; // endl is a Printable
print(_t);
Here DefaultPrintable is a class that implements the Printable interface,
with the same functionality as your PrintParam class, and endl is a constant
object of a class that implements Printable.
You can have also a PrintParam, interface that extends Printable, used by
print to modify the aspect of the next parameter to print.
Still I don't know what there should be inside the Printable interface:
interface Printable
{
???
}
And how print() can print an array of Printables?
Ciao

Why not adding some intelligence to the compiler?
int print(..., Printable[] args, DefaultPrintable)
Here Printable is an interface, and the compiler does this:
for each parameter Pi
{
if Pi is a Printable
then args[i] = Pi
else args[i] = new DefaultPrintable(Pi)
}
So the example:
MyClass d = something(); // MyClass is a Printable
print("The value is: ", d, endl);
becomes:
Printable _t[];
_t[0] = new DefaultPrintable("The value is: "); // char[] is not a Printable
_t[1] = d; // d is a Printable
_t[2] = endl; // endl is a Printable
print(_t);
Here DefaultPrintable is a class that implements the Printable interface,
with the same functionality as your PrintParam class, and endl is a constant
object of a class that implements Printable.
You can have also a PrintParam, interface that extends Printable, used by
print to modify the aspect of the next parameter to print.
Still I don't know what there should be inside the Printable interface:
interface Printable
{
???
}
And how print() can print an array of Printables?
Ciao

Clever idea, but it's not very efficient, as it involves the compiler
allocating an interface for every argument passed, whether or not it's a
PDT.
Something else that would cut it is some sort of template list where
each element has its own type. (think C++ typelist) Then you could
recursively go through the list, and let template specialization figure
out what to call for each argument. Obviously, D templates aren't yet
up to this task.
Barring that, it would be quite useful if it were possible to have a
function that accepts any number of arguments (all of the same type),
and pass them as an array. ie
void foo(char[] s, params int[] args) { ... }
...
foo("Hurrah", 5, 2, 8, 9, 7, ...);
Of course, this is nearly useless in the case of string formatting (too
many toString calls needed), but it would still be useful in a great
number of other situations. Further, even when writing/using a
formatted printing function, params Object[] would be nearly ideal.
(except for the problem of dumping PDTs in pointless wrapper classes, a
la Java)
-- andy

Seeing recent discussions like "formatted output trial balloon",
"stream operator suggestions", "printf and read/write a proposal",
etc., I'm encouraged to post my proposal for type-safe varargs again -
since I still think it's a Good Idea.
Here's an example of the syntax for a type-safe varadic function:
int print(PrintParam args...)
What this says is that the function "print" takes a variable number of
arguments, and each is of type "PrintParam", and they have been
collected into a dynamic array "PrintParam args[]".
So imagine we have:
class PrintParam
{
this(char[] arg) { /* build a PrintParam from a string */ }
this(int arg) { /* build a PrintParam from an int */ }
/* etc, ad infinitum */
....then construction of this array is done automatically by the
compiler when the user invokes the function "print", something like this:
int d = 2;
print("The value of D is ", d, "\n");
(Oh, by the way: Notice that this is the Perfect Syntax for a print
function - just arguments, separated by commas.)
....then the compiler produces code equivalent to this:
PrintParam _t[];
_t[0] = new PrintParam("The value of D is ");
_t[1] = new PrintParam(d);
_t[2] = new PrintParam("\n");
print(_t);
The compiler coerces each argument into the proper type, in this case
PrintParam. It does so by looking for a constructor which can be used
to build a PrintParam (if the argument isn't already compatible with
PrintParam, that is).