guard clause style static if
Timoses
timosesu at gmail.com
Tue Jul 10 12:56:39 UTC 2018
On Tuesday, 10 July 2018 at 12:05:11 UTC, kdevel wrote:
> On Saturday, 7 July 2018 at 13:12:59 UTC, Alex wrote:
>> The site you cited for the guard clause above (c2.com)
>> works at runtime.
>
> ?
static if works at compile team and only inserts code into the
final code for run-time depending on the condition (which has to
be known at compile time). In your case
void bar (T ...) (T args)
{
static if (args.length == 0)
return;
writeln (args [0]);
return bar (args [1 .. $]);
}
you could also write
static if (T.length == 0)
so in case T.length == 0 the resulting run-time code would yield
void bar (T args) // in that case T is nothing
{
return;
writeln (args [0]);
return bar (args [1 .. $]);
}
So the only thing you can control with static if statements is
"what code to run" depending on the template arguments.
The problem with the error messages you are getting
(https://forum.dlang.org/post/yndsroswikghknzlxwqi@forum.dlang.org) is that the compiler checks during compilation time whether `args[0]` is valid (which in above case it is not). So you can't use args like a "normal array" as you would in control statements during run-time. I don't know what exactly `args` represents in the background.
> [...]
More information about the Digitalmars-d-learn
mailing list