pure or not pure?
Steven Schveighoffer
schveiguy at yahoo.com
Wed Apr 9 12:46:08 PDT 2008
I realize that I don't fully understand all the rules of what D pure
functions will be.
I think all agree on these rules:
- pure functions can only call pure functions
- allow access to invariant data
- allow mutable access to simple stack variables (variables that are all on
the stack, no references)
In Andrei's accu-functional document, instead of the last rule, there is:
- allow local automatic mutable state
I'm not sure what this means :) But here are some questions about what is
pure and what is not pure:
1. Can pure functions use mutable heap data? If so, what are the
restrictions for this?
i.e.:
pure char[] f(int x, int y)
{
char[] c = new char[y];
c[] = (char)x;
}
pure char[] g(int x)
{
char[] c = f(x, 5);
}
2. Can pure functions use stack references to objects that are partially
invariant, partially mutable?
i.e.:
class C
{
invariant int x;
int y;
}
pure int f(C c) {return c.x} // allowed?
This of course, assumes that C can assign x through a constructor, not just
through a static initializer (this is not implemented today).
as an example of a pure function that takes a pointer to mutable data, but
doesn't use the data, i.e. this doesn't ever read or write heap data:
pure char *add(char * c, int n) { return c + n;}
3. If the answer to question 1 is 'yes', can you pass mutable heap data to
pure functions?
pure char[] substr(char[] src, int beg, int end) { return src[beg..end];}
Would substr be allowed to be called from a pure function?
Would substr be allowed to be called from a non-pure function?
Will the compiler tag heap data somehow during execution of a pure function
to determine whether it is unique or not?
I'm just trying to get a handle on what is and isn't considered pure, as
everyone seems to have a different opinion...
-Steve
More information about the Digitalmars-d
mailing list