How to use pure in D 2.0 question on Stack Overflow?
Denis Koroskin
2korden at gmail.com
Thu Jun 18 00:35:29 PDT 2009
On Thu, 18 Jun 2009 00:32:20 +0400, Jeroen Dirks
<jeroen.dirks at sympatico.ca> wrote:
> Does anyone know the answer to this D related question on SO?
>
> http://stackoverflow.com/questions/1008803/how-to-use-pure-in-d-2-0/
There needs to be a way to tell that addMsg doesn't access global
variables.
One of the solutions is to add one more annotation (I call it impure here,
because it is an exact opposite of pure):
void foo() // doesn't access globals, fine to call inside pure functions
{
}
void bar() // fails to compile, accesses global state but doesn't have
proper annotation
{
errno = 42;
}
impure void bar() // fine
{
errno = 42;
}
But there might be a problem: calling impure function makes your function
impure, too, so it's viral.
Another solution is... remove global variables entirely! No globals - no
problems :)
More information about the Digitalmars-d
mailing list