Do pure functions solve the "return const" problems?
Russell Lewis
webmaster at villagersonline.com
Tue Apr 1 12:07:34 PDT 2008
Steven Schveighoffer wrote:
> 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?
Interesting question, I don't really know the answer. However, it seems
to me that "pure" is a statement that you are making about the function,
not necessarily about the caller. That is, "pure" means "this function
does not have side-effects, so use it with impunity," whereas
"invariant" means "the caller must obey rules that the function is
relying on."
Put another way, saying that a function is "pure" allows the caller to
make certain optimizations. Saying that the arguments are invariant
allows the pure function to optimize internally.
I think that it might be quite possible to have a pure function which
doesn't require invariant arguments...it would just lose out on internal
optimization opportunities.
More information about the Digitalmars-d
mailing list