It seems pure ain't so pure after all

Jonathan M Davis jmdavisProg at gmx.com
Sun Sep 30 23:18:45 PDT 2012


On Monday, October 01, 2012 08:09:38 Tommi wrote:
> On Monday, 1 October 2012 at 06:01:24 UTC, Jonathan M Davis wrote:
> > It would be kind of like complaining that a pure function
> > returns different
> > values on Linux and Windows due to a version statement or
> > static if. It's just
> > that in the thing that varies is compile time vs runtime not
> > the target
> > machine. e.g.
> 
> Actually... let's not even worry about the definition of the word
> 'pure'. Let's just ask ourselves: do we really want to live in a
> world where people can write code like that:
> 
> void main()
> {
>      auto x = pow2(3);
>      enum y = pow2(3);
> 
>      assert(x == y + 3);
> 
>      writeln("Take that mr. \"math\" professor!");
>      readln();
> }

Then don't write a function which claims to square a value and does something 
else. That's just bad naming. No language is going to prevent programmers from 
being idiots.

A function which uses __ctfe should probably do essentially the same thing at 
both runtime and compile time, but it _has_ __ctfe, because the runtime 
implementation won't work at compile time, and it's up to the programmer to 
make sure that the function does what it's supposed to at both compile time 
and runtime. The compiler can't possibly enforce that.

- Jonathan M Davis


More information about the Digitalmars-d mailing list