www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Purity not enforced for default arguments?

reply "Xinok" <xinok live.com> writes:
The following code fails to compile because unpredictableSeed is 
impure:

     void main()
     {
         foreach(i; 0..10) writeln(pureRandom);
     }

     pure uint pureRandom()
     {
         auto r = Random(unpredictableSeed);
         return r.front;
     }

However, make unpredictableSeed a default argument, wrap the call 
in another pure function, and it compiles:

     void main()
     {
         foreach(i; 0..10) writeln(pureRandom2);
     }

     pure uint pureRandom2()
     {
         return pureRandom;
     }

     pure uint pureRandom(uint seed = unpredictableSeed)
     {
         auto r = Random(seed);
         return r.front;
     }

I'm inclined to believe this is a bug. While pureRandom could be 
considered weakly pure, pureRandom2 has no arguments so it should 
be strongly pure. Yet, it yields a different value on every call.
Mar 10 2015
parent reply "safety0ff" <safety0ff.dev gmail.com> writes:
On Tuesday, 10 March 2015 at 21:56:39 UTC, Xinok wrote:
 I'm inclined to believe this is a bug.
https://issues.dlang.org/show_bug.cgi?id=11048
Mar 10 2015
parent "Xinok" <xinok live.com> writes:
On Tuesday, 10 March 2015 at 22:00:29 UTC, safety0ff wrote:
 On Tuesday, 10 March 2015 at 21:56:39 UTC, Xinok wrote:
 I'm inclined to believe this is a bug.
https://issues.dlang.org/show_bug.cgi?id=11048
Thanks for the link, and I didn't mean to post this in D.learn.
.<
Mar 10 2015