Idea: partially pure functions

Bruno Medeiros brunodomedeiros+spam at com.gmail
Wed Apr 30 04:14:11 PDT 2008


Don wrote:
> Bruno Medeiros wrote:
>> I want to conceptualize an idea that has been briefly mentioned in the 
>> previous pure discussions (by Don I think?), but has not been 
>> explicitly brought into attention.
>>
>> As we know, the current definition of pure functions (according to 
>> http://www.digitalmars.com/d/2.0/accu-functional.pdf) is that they can 
>> only access invariant, or local mutable data. This means that all of 
>> the function's parameters have to be invariant.
>>
>> The idea is: let's also allow pure functions to have (and access) 
>> non-invariant parameters. These won't be normal pure functions, but 
>> instead "partially" pure functions. Their semantics are: the function 
>> promises not to change any external/global data, except for the 
>> parameters that are not invariant. Example:
> 
> The first case to consider is nested functions.
> 

Nested functions are a more complicated case than regular functions, as 
they have other "inputs" other than the parameters: the outer variables.
So I think that if you implement partial pure semantics for nested 
functions, then you have it working for normal functions as well.

> 
> But Walter's already said that pure functions will start out very 
> restricted, and the rules will be relaxed over time. So it's not really 
> worth worrying about the rules right now.

True, if we're thinking about compiler implementation only. But in terms 
of design, this might not be just an improvement, it could a crucial 
functionality. For example, consider this other example: Suppose you 
have a pure function with a local object and you want to mutate that 
object using a mutable method. If you are not able to call the mutable 
method in the pure function, you might not even be able to describe the 
method changes inside the pure function, because of Object encapsulation 
(example: changing a private member).

So partial pure semantics might be necessary if one wants pure functions 
to be able to work with objects in the general sense.

-- 
Bruno Medeiros - Software Developer, MSc. in CS/E graduate
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list