Purity

Bruno Medeiros brunodomedeiros+spam at com.gmail
Fri Feb 11 05:55:06 PST 2011


On 18/12/2010 12:46, Don wrote:
> spir wrote:
>> On Sat, 18 Dec 2010 01:08:20 -0800
>> Jonathan M Davis <jmdavisProg at gmx.com> wrote:
>>
>> Thank you for the explanation about strongly pure funcs calling weakly
>> pure ones --this fully makes sense.
>>
>>>> I would like weakly pure to include output funcs, and exclude all
>>>> possibilities to modify (non-local) state.
>>> The problem is that output is accessing global variables - which
>>> weakly pure functions _cannot_ do.
>>
>> Why? What is the rationale for excluding output (I don't mean I/O,
>> only O)?
>
> You're correct in saying that it doesn't affect the operation of the
> program. But in practice, program output is almost always important.
>
> For example, suppose we allowed output to be pure. Then consider:
>
> writeln("Hello, world!");
>
> Since it returns nothing, and has no influence on the future execution
> of the program, the writeln can be dropped from the program.
>
> Hmmm....


Hum, it might still be useful to have something like a compiler switch 
that disables pure altogether, then people could use I/O and other 
non-pure operations for debugging purposes. One could wrap such code 
with a version statement:

void myPurefunc(Foo foo) pure {
   version(pure_disabled) {
     writeln("some debug info: ", foo);
   }
   //...


-- 
Bruno Medeiros - Software Engineer


More information about the Digitalmars-d mailing list