Most basic nothrow, pure, @safe functions?

Meta jared771 at gmail.com
Fri Mar 21 12:55:47 PDT 2014


On Friday, 21 March 2014 at 19:19:06 UTC, Walter Bright wrote:
> Here's a litmus test you can use for purity. Consider:
>
>     int foo() pure;
>     {
> 	auto a = foo();
>     }
>
> Now, since 'a' is never used, we can delete the assignment, and 
> since foo is pure, we can delete foo():
>
>     {
>     }
>
> Is this program distinguishable from the former? If not, then 
> foo() is not pure.

Consider this:

int foo(int* pa) pure
{
     if (pa)
     {
         *pa = foo();
     }
}

This is also pure according to D's type system, but it fails your 
litmus test. This, however, passes:

int foo(immutable(int)* pa) pure
{
     pa = pfoo();
}

I think the case of a monitor is closer to the first example.


More information about the Digitalmars-d mailing list