Can pure functions accept const parameters or only invariant ?

mastrost titi.mastro at
Mon Dec 29 13:36:50 PST 2008

I read in the doc that pure functions can only accept invariant parameters, but it seems that const parameters are also accepted by the compiler. For instance:

import std.stdio;

class A{
    int x;

pure int f(const A a){
    //- as const is transitive, we cannot modify any part of 'a' from here.
    //  b u t:
    //- as 'a' is not invariant, it may be changed in the same time by a non constant view of this
    //  same data
    return a.x;

void main(){
    A a= new A; //a is not invariant

So why is this code accepted by the compiler?

More information about the Digitalmars-d mailing list