On Thursday, 21 May 2015 at 11:05:10 UTC, Dennis Ritchie wrote:
Is it possible to write something like this in the compile time?
auto foo(T)(T t)
if (is(T == immutable(int)) && t == 5)
// Error: variable t cannot be read at compile time
{
// ...
}
void main()
{
immutable(int) n = 5;
foo(n);
}
Although the compiler already has enough information, this is not
possible because the template constraints are not allowed to use
the runtime parameters (they are compile-time only constraints).
It would be more doable like this:
auto foo(T)(T t)
if (is(T == immutable(int)))
{
assert(t == 5);
}
void main()
{
immutable int n = 6;
foo(n); // Possible in the future:
// dmd test.d --deep-asserts-verification
// Error: function foo has an assert which can't satisfied:
// assert(5 == n) <- n is 6
}