Semantics of mixed CT and RT statements

Neia Neutuladh neia at ikeran.org
Mon Dec 24 01:14:41 UTC 2018


On Mon, 24 Dec 2018 01:08:57 +0000, Timoses wrote:
> Do you have an example? I'm not quite sure I get what you mean.

bool foo(T)(T t) if (is(T == int) || is(T == string))
{
  if (is(T == int) || t == T.init) return false;
  else return t.startsWith("something");
}

This is a pretty contrived example, but the is() constrains the type in 
the else clause. The else clause has to type-check for both int and 
string, and it doesn't.

The fix is straightforward: detangle the runtime and compile-time 
conditions.

bool foo(T)(T t) if (is(T == int) || is(T == string))
{
  static if (is(T == int))
  {
    return false;
  }
  else  // T == string
  {
    if (t == T.init) return false;
    return t.startsWith("something");
  }
}


More information about the Digitalmars-d mailing list