It seems pure ain't so pure after all
Alex Rønne Petersen
alex at lycus.org
Sun Sep 30 22:43:59 PDT 2012
On 01-10-2012 07:40, Tommi wrote:
> import std.stdio;
>
> int pow2(int val) pure
> {
> if (__ctfe)
> return 6;
> else
> return val * val;
> }
>
> void main()
> {
> assert(pow2(3) == 9);
> static assert(pow2(3) == 6);
>
> writeln("9 = 6 ... I knew it! '6' was faking it all along");
> readln();
> }
This is a corner case.
__ctfe is there to allow special-cased CTFE code when absolutely
necessary. By necessity, this separates a function into two worlds:
compile time and run time.
As far as purity goes, pow2 *is* pure. It just does something different
depending on whether you run it at compile time or run time. I don't see
this as a problem in practice.
--
Alex Rønne Petersen
alex at lycus.org
http://lycus.org
More information about the Digitalmars-d
mailing list