Purity not enforced for default arguments?
Xinok via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Mar 10 14:56:38 PDT 2015
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.
More information about the Digitalmars-d-learn
mailing list