Semantics of mixed CT and RT statements

Timoses timosesu at gmail.com
Sat Dec 22 03:35:56 UTC 2018


I've just had the idea of mixing both `static` and "RT" 
statements:


     void foo (T)(bool b)
     {
         static if (is(T == SomeType)) || if (b)
         {
             // Called when T is of type SomeType
             // otherwise, use `if (b)` for runtime to decide
             // fate of this block's execution
             assert(is(T == SomeType) || b);
             writeln("1");
         }
         else static if (is(T == SomeOtherType)) && if (b)
         {
             // Called when T is of type SomeOtherType
             // AND when b is true
             assert(is(T == SomeOtherType) && b);
             writeln("2");
         }
     }

     foo!SomeType(false); // prints 1
     foo!SomeOtherType(false); // prints nothing
     foo!SomeOtherType(true); // prints 2


static if (A) || if (B): Only ever check B at run-time when A is 
false at compile-time

static (A) && if (B): Check B at run-time if and only if A holds 
true at compile-time.


And here both variants (as had to be implemented currently versus 
the shorter "mixed" variant)

     // Current
     static if (is(T == SomeType))
         // do A
     if (b)
         // do A
     static if (is(T == SomeOtherType))
     {
         if (b)
             // do B
     }

     // vs "Mixed"
     static if (is(T == SomeType)) || if (b)
         // do A
     static if (is(T == SomeOtherType)) && if (b)
         // do B


I don't have a specific use case at hand. I believe it could help 
reduce code duplication in some situations; apparently more so 
for the `||` variant, as the `&&` variant can be rewritten as:

     static if (Cond1) if (Cond2)

and works already.


What do you think?



More information about the Digitalmars-d mailing list