mutable, const, immutable guidelines

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Thu Oct 10 16:32:37 PDT 2013


On 09/10/13 17:50, Daniel Davidson wrote:
> Take, for example, LinearCongruentialEngine from random.d. It has a function:
>
>      bool opEquals(ref const LinearCongruentialEngine rhs) const
>
> Why is it using const here instead of immutable?

Among other things, because no LinearCongruentialEngine will ever, ever be 
immutable -- because it won't work if it's immutable.  Whereas ref const I 
believe is simply saying, "You can't mutate it via this reference."  I'm not 
sure if it ought technically to be auto ref const.

In any case, I think the general reason is in order to be agnostic about type 
qualifications.  Try:

     BigInt a = BigInt(5);
     immutable BigInt b = immutable(BigInt)(5);
     writeln(a.opEquals(b));

... which works.  BigInt.opEquals takes input by an auto ref const.


More information about the Digitalmars-d-learn mailing list