It seems pure ain't so pure after all

Graham Fawcett fawcett at uwindsor.ca
Mon Oct 1 11:31:56 PDT 2012


On Monday, 1 October 2012 at 17:53:35 UTC, Tommi wrote:
> On Monday, 1 October 2012 at 08:00:47 UTC, Jakob Ovrum wrote:
>>
>> The only real problem here is that you wrote a function called 
>> pow2 that effectively returns 6 unconditionally. I doubt your 
>> math professor would be particularly impressed. If you had 
>> used __ctfe properly, it would return the same value both at 
>> compile-time and runtime. At present, __ctfe is a necessary 
>> evil as CTFE can be severely crippled without it.
>
> I solemnly swear not to use __ctfe improperly. But my problem 
> with it is, that there *exists* the possibility of improper use 
> of __ctfe.

Can you name a programming language that supports CTFE, but 
doesn't allow the possibility of misuse?

Actually, can you name any language with a safety or purity 
feature that cannot be thwarted by a motivated programmer?

To echo Jonathan's sentiment, no programming language can stop a 
programmer who is determined to write stupid code from doing so.

For the sake of discussion, here's an example of a pure function 
in Haskell (arguably the "purest" language in wide use today) 
that happily returns a random integer every time it is called.

import System.IO.Unsafe
import System.Random

pureFunction :: () -> Int
pureFunction x = unsafePerformIO impureInside
     where impureInside = getStdRandom (randomR (1,6))

main = do
   print (pureFunction ())
   print (pureFunction ())
   print (pureFunction ())

This prints three random integers between 1 and 6 when executed.

-- Graham



More information about the Digitalmars-d mailing list