Why is length being tested on an int?
Shriramana Sharma
not.sure.why.a.mail.id.is.needed.even.if.invalid at gmail.com
Sat Oct 28 02:38:43 UTC 2017
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!
More information about the Digitalmars-d-learn
mailing list