Sat, 06 Nov 2010 13:49:20 +0200, Vladimir Panteleev wrote: > A while ago, someone added an example with pure functions to Wikipedia's > D article: > > http://en.wikipedia.org/wiki/D_(programming_language)#Functional > > Someone on the talk page asked why does the program compile despite that > mySum accesses a variable in its enclosing function: > > http://en.wikipedia.org/wiki/Talk:D_(programming_language) #Purity_of_mySum_function_in_the_.22Functional.22_section_.281.1.4.29 > > I replied with: > >> The code indeed compiles. I think that the idea is that nested >> functions have a hidden argument - a pointer to their enclosing scope >> (main's local variables). However, that doesn't explain why the code >> continues to compile when pivot is moved outside main(), or if you add >> a call to a non-pure function in mySum - these sound like compiler >> bugs. > > I don't know much about purity, so I thought someone could shed some > light on this?
The function isn't referentially transparent, at least. Well, it is in that particular example if the compiler can "infer" the constness of 'pivot'. Otherwise the value requires a constness attribute. Depends on whether we are splitting hair again, right? auto x = my_sum(a,b); pivot = 42; auto y = my_sum(a,b); assert(x == y); // fails