To help LDC/GDC

Simen Kjaeraas simen.kjaras at gmail.com
Mon Apr 8 11:14:51 PDT 2013


On 2013-04-08, 17:07, Jacob Carlborg wrote:

> On 2013-04-08 14:52, Iain Buclaw wrote:
>> On 8 April 2013 13:25, Jacob Carlborg <doob at me.com <mailto:doob at me.com>>
>> wrote:
>>
>>     On 2013-04-08 10:29, Iain Buclaw wrote:
>>
>>         This information could possibly be helpful.  Though given that
>>         most of
>>         (gdc) codegen is on par with g++, there's probably not much on
>>         the list
>>         that isn't already detected by the backend optimisation passes.
>>
>>
>>     Multiple calls to pure functions could be cached.
>>
>>     --
>>     /Jacob Carlborg
>>
>>
>> Not always, but in some circumstances, yes.
>>
>> ---
>> struct Foo
>> {
>>    int a = 0;
>>    pure int bar (immutable int x)
>>    {
>>      ++a;
>>      return x * 2;
>>    }
>> }
>>
>>
>> void main()
>> {
>>    Foo f;
>>    int i = f.bar(2) + f.bar(2);
>>
>>    assert (i == 8);
>>    assert (f.a == 2);
>> }
>
> I though that wasn't possible. What's the point of pure if that's  
> possible?

Like others have stated, it's so you can do this:

struct Foo {
    int a = 0;
    pure int bar( int n ) { // Weakly pure
        a += n;
        return a;
    }
}

pure int Baz( int n ) { // Strongly pure
     Foo foo;
     return foo.bar( n );
}

Foo.bar has only local mutability, so if the Foo originated in a
(strongly) pure function, Foo.bar can be called inside that pure
function without violating the purity requirement.

The three levels (weakly pure/strongly pure/impure) are needed,
but a redesign would perhaps use a different keyword for weakly
pure.

-- 
Simen


More information about the Digitalmars-d mailing list