On 12/23/20 1:06 AM, Godnyx wrote:
> for (ulong i = 0; i < args.length; i++) {
> if (typeof(args[i]).stringof == "string")
> printf("%s\n", args[i].toStringz);
> }
I replaced for with foreach and it worked (and I passed "prompt").
static foreach would work as well.
import core.stdc.stdio : printf;
import std.string : toStringz;
void put(A...)(string prompt, A args) {
foreach (arg; args) {
if (typeof(arg).stringof == "string")
printf("%s\n", arg.toStringz);
}
}
void main() {
string h = "World!";
string w = "World!";
put("prompt", h, w);
}
But it can get better: you don't want to compare typeof with "string" at
run time. Instead, there shouldn't be any code generated for the
non-string cases. Enter 'static if' and the 'is' expression to feel
better. :)
import core.stdc.stdio : printf;
import std.string : toStringz;
void put(A...)(string prompt, A args) {
static foreach (arg; args) {
static if (is (typeof(arg) == string)) {
printf("%s\n", arg.toStringz);
}
}
}
void main() {
string h = "World!";
string w = "World!";
put("prompt", h, w);
}
Ali