guard clause style static if

kdevel kdevel at vogtner.de
Tue Jul 10 14:14:30 UTC 2018


On Saturday, 7 July 2018 at 11:56:40 UTC, rikki cattermole wrote:
>    void bar (T ...) (T args) if (T.length == 0)
>    {
>       return;
>
[...]
>    }
>
>    void bar (T ...) (T args) if (T.length > 0)
>    {
>       writeln (args [0]);
>       return bar (args [1 .. $]);
>    }

This is a version without a second condition:

    import std.stdio;

    void foo ()
    {
       writeln;
    }

    void foo (T ...) (T args) if (T.length > 0)
    {
       writeln ("arg = ", args [0]);
       foo (args[1 .. $]);
    }

    void main ()
    {
       foo ();
       foo (1);
       foo (1, "2");
       foo (1, 2, 3.);
    }



More information about the Digitalmars-d-learn mailing list