Do pure functions solve the "return const" problems?

Steven Schveighoffer schveiguy at yahoo.com
Tue Apr 1 11:40:55 PDT 2008


"Russell Lewis" wrote
> I've been pondering the discussions about "return const" and such. 
> Basically, the idea is to be able to define a function which is not 
> allowed to modify certain data, but which can return subsets of that data 
> in a non-const fashion.  Classic examples are strstr (scan, but don't 
> alter, the data, but return a usable pointer) and min/max (compare the 
> items and return one, but return non-const versions of them).
>
> Are pure functions the way?  Isn't a pure function required to not modify 
> the data?  And, for that reason, can't a pure function be utterly ignorant 
> of the const-ness of the data?
>
>
> BEGIN CODE
> pure char[] strstr(char[] haystack, char[] needle)
> {
> // perform the search
> int indx = <whatever>
>
> return haystack[indx..$];
> }
> END CODE

Having never used pure functions, I'm not sure that this is the case for 
C++'s pure functions, but I thought pure functions for D would require 
invariant arguments, no?  Otherwise, how do you guarantee another thread 
doesn't come along and munge the data while the pure function is running?

-Steve 





More information about the Digitalmars-d mailing list