Hello. I want a function to be able to take any arguments like
write() and print them out but quoting string arguments of length
more than 1. So I write the following quote:
import std.stdio;
string myFunc(string arg) { return '\"' ~ arg ~ '\"'; }
void myWrite(T ...)(T args)
{
foreach (arg; args)
if (is(typeof(arg) == string) && arg.length > 1)
write(myFunc(arg));
else
write(arg);
}
void main() { myWrite("Hello", 1, "c"); }
However I am getting the error even when compiling:
<src>(6): Error: no property 'length' for type 'int'
<src>(7): Error: function <src>.myFunc (string arg) is not
callable using argument types (int)
<src>(11): Error: template instance <src>.myWrite!(string, int,
string) error instantiating
I am not sure why, given short circuit evaluation, it is testing
the length of the int argument? Or is it that the problem is at
compile time itself so short circuit doesn't come into play?
How to rewrite the function so I don't get the error?
Thanks!