dlang.org/spec/function.html#pure-functions example

Jonathan M Davis newsgroup.d at jmdavisprog.com
Thu Oct 12 21:20:44 UTC 2023


On Thursday, October 12, 2023 1:33:32 PM MDT Paul via Digitalmars-d-learn 
wrote:
> The spec doc has the following statement and corresponding
> example:
> ***"Pure functions cannot directly access global or static
> mutable state."***
>
> ```d
> int x;
> immutable int y;
>
> pure int foo(int i)
> {
>      i++;     // ok, modifying local state
>      //x = i; // error, modifying global state
>      //i = x; // error, reading mutable global state
>      i = y;   // ok, reading immutable global state
>      throw new Exception("failed"); // ok
> }
> ```
> If **int x** is global mutable state, what does static mutable
> state look like?

Types can have static members.

Basically what it comes down to is that outside of immutable data, pure
functions only have access to their arguments and to what they can access
via their arguments (be it by getting pointers from those arguments or
calling other pure functions on them).

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list