Semantics of mixed CT and RT statements

Rubn where at is.this
Sat Dec 22 03:51:58 UTC 2018


On Saturday, 22 December 2018 at 03:35:56 UTC, Timoses wrote:
> 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?

Don't really see the point, the following will always be true 
because of the static if.

>         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");
>         }

You don't need to do another is() inside of the assert, as it is 
guaranteed to be true. Just put the logic in the static if like 
normal.

static if( is(T== SomeType) ) {
     // runtime condition
     if( true || b ) // just becomes true
         writeln("1");
}
else static if( is (T == SomeOtherType ) ) {
     if( b ) {
         writeln("2");
     }
}


More information about the Digitalmars-d mailing list