Partially pure (Re: Fully transitive const is not necessary)
Walter Bright
newshound1 at digitalmars.com
Fri Apr 4 01:56:25 PDT 2008
Janice Caron wrote:
> What about this one. Is f pure?
>
> import std.date;
>
> void main()
> {
> invariant s = toString(getUTCtime());
>
> pure string f()
> {
> return s;
> }
>
> writefln(s);
> }
>
> The function f relies only on the string s. which is fully invariant
> and in scope. Yet f will give a different return value for the same
> input (void), each time the program is run.
>
> I assume the answer is no, but this one seems a bit grey to me. Is it
> impure because s is not global?
f() is pure because it relies on invariant data. Invariant data can be
initialized at runtime, as long as it is before any pure functions
attempt to read it.
More information about the Digitalmars-d
mailing list