purity and memory allocations/pointers

monarch_dodra monarchdodra at gmail.com
Sat Aug 3 08:59:21 PDT 2013


I'm a bit confused about where we draw the line with purity, and 
memory.

For example, take this "simple" function:

//----
char[] getMutableHello() pure
{
     return "hello".dup;
}
//----

This seems like it will *always* create the same result, yet at 
the same time, it calls the GC. "dup" could fail, amongst others, 
meaning there is failed purity.

Is this OK?

:::::::::::::::::::::::::::::::::::::::::

Where does purity mean when you are talking about 
slices/pointers? Simply that they *point*/*reference* the same 
payload? I mean:

void main()
{
     auto a = getMutableHello();
     auto b = getMutableHello();
     assert(a == b); //OK
     assert(a is b); //Derp :/
}

Did getMutableHello just violate its purity promise?

:::::::::::::::::::::::::::::::::::::::::

Another thing that bothers me, again, when interfacing with the 
GC, is "capacity". It is currently marked as pure, but I believe 
this is wrong: The GC can in-place extend the "useable" memory of 
a dynamic array. This means all referencing slices will be 
affected, yet un-modified. This means that calling "s.capacity" 
depends on the global state of the GC:

//----
     auto s1 = new ubyte[](12_000);
     auto bck = s1;
     size_t cap = s1.capacity;
     s1.reserve(13_000);
     assert(s1 is bck); //Apparently, s1 is not changed.
     assert(cap == s1.capacity); //This fails, yet capacity is 
pure?
//----

Again, is this OK?

:::::::::::::::::::::::::::::::::::::::::

One last question: Pointers.

int get(int* p) pure
{
     return *p;
}

void main()
{
     int i = 0;
     auto p = &i;
     get(p);
}

Here, get, to me, is obviously not pure, since it depends on the 
state of the global "i". *Where* did "get" go wrong? Did I simply 
"abusively" mark get as pure? Is the "pure" keyword's guarantee 
simply "weak"?

:::::::::::::::::::::::::::::::::::::::::

To sum up the question, regardless of how the *keyword* pure 
currently works, my question is "*How* should it behave"? We're 
currently marking things in Phobos as pure, and I'm worried the 
only reason we are doing it is that the keyword is lenient, and 
said functions *aren't* actually pure...


More information about the Digitalmars-d mailing list