Can pure functions accept const parameters or only invariant

mastrost titi.mastro at free.fr
Mon Dec 29 16:37:55 PST 2008


Christopher Wright Wrote:

> mastrost wrote:
> > So why is this code accepted by the compiler?
> 
> I'm not sure whether it's intentional, but a pure function is correct 
> for mutable input as well as invariant input (barring threading issues), 
> so this avoids code duplication.

Yes maybe, but it creates strange stuffs... you could have a pure function taking const inputs, another pure function taking invariant inputs, and of course the first one could not call the second one, even if it was called with invariant inputs.....I mean this would not be allowed:

pure int g(invariant A a){
    return a.x;
}

pure int f(const A a){
    assert(is(typeof(a) == const(A))); //a is seen as const, even if a is in fact invariant 
    return g(a); //error
}

Wich means that it is possible that a pure function can not call another one. 




More information about the Digitalmars-d mailing list