Idea: partially pure functions
Bruno Medeiros
brunodomedeiros+spam at com.gmail
Thu May 1 08:13:49 PDT 2008
Steven Schveighoffer wrote:
> "Bruno Medeiros" wrote
>> I state that if you have 'pure' implemented (in a sensible manner), it
>> takes zero effort to implement partial/contextual pure.
>>
>> The definition of partial pure functions requires no extra effort from the
>> compiler to check. (The *same* check for normal pure functions is made:
>> that no external state is accessed unless invariant)
>>
>> The usage of partial pure functions also requires no extra effort. Inside
>> a pure function body, the compiler already has to check if no external,
>> non-invariant variables are accessed, like this:
>>
>> Foo globalVar;
>>
>> pure void func(...) {
>> localVar = globalVar + 1; // not allowed
>>
>> then just apply this same check on *any* expression, including function
>> call expressions:
>>
>> partiallyPureFunction(localVar, globalVar); // not allowed
>> partiallyPureFunction(localVar, localVar2); // but this is allowed
>
> What if localVar is a pointer to global data, or transitively contains a
> pointer to global data? There is no way to know whether localVar is a
> pointer to global data or 'contained' heap data without lots of context
> checking.
Whatever checks you have to make for localVar, you still have to do them
with normal pure. (And not that it matters to the point, how do you
initialize a local var with global data?)
> If partially pure functions with mutable parameters were allowed,
> I'd say it's a requirement that you can only call them from partially or
> fully pure functions, as local pointers from those functions are guaranteed
> to be contained within the function.
>
I'm not sure I understand what you're saying. Are you saying that
partially pure functions should not be called from *non-pure* functions?
What's the sense in that?
--
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