Constant function declaration: is it cheating?
Posted Jun 13, 2008 16:31 UTC (Fri) by
giraffedata (subscriber, #1954)
Parent article:
Implications of pure and constant functions
Is it really cheating or trickery to declare a function constant even though it accesses the larger environment? Or is it just helping the compiler properly classify the function when it doesn't have enough information to do it itself?
All programs make assumptions about their environment. For example, that there isn't another thread running around overwriting the memory in which the program's variables are stored. Where the user doesn't assure that assumed environment, the program's behavior is arbitrary.
So isn't it legitimate to make the environmental assumption that a certain file's contents don't change while the program runs? With that assumption, a function whose result depends upon the contents of the file is constant.
(
Log in to post comments)