Thread overview
Purity not enforced for default arguments?
Mar 10, 2015
Xinok
Mar 10, 2015
safety0ff
Mar 10, 2015
Xinok
March 10, 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.
March 10, 2015
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
March 10, 2015
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. >.<