pure functions without invariant (a get-rid-of-const proposal)

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Sun Mar 8 16:11:46 PDT 2009


I'm pretty much in agreement with Andrei and BCS here, so I'll just 
comment on this bit:

hasen wrote:
> When the coder marks a function as "pure", the compiler must check that 
> it actually is pure.
> I think the compiler can do this, in a way similar to how it can detect 
> which functions can have compile time function execution (CTFE)

The DMD[1] doesn't actually check whether it can execute a function at 
compile time, it just tries to do it and errors out if it comes across 
something it can't handle.

For example, this compiles and prints '100':
-----
module test;

uint y = 100;
uint* x = &y;

uint foo(bool readptr) {
     if (readptr)
         return *x;
     return 100;
}

// Doesn't compile if changed to foo(true):
// test.d(14): Error: cannot evaluate foo(true) at compile time
auto bar = foo(false);

void main() {
     printf("%d\n", bar);
}
-----
But as the comment states, changing the parameter to true makes it error 
out because it can't do CTFE on foo(true), even though it managed fine 
with foo(false).


[1]: and LDC/GDC, which use the same frontend.



More information about the Digitalmars-d mailing list