forcing weak purity
deadalnix
deadalnix at gmail.com
Wed May 23 07:42:08 PDT 2012
Le 23/05/2012 16:03, deadalnix a écrit :
> Le 23/05/2012 15:57, Steven Schveighoffer a écrit :
>> On Wed, 23 May 2012 09:52:31 -0400, deadalnix <deadalnix at gmail.com>
>> wrote:
>>
>>> Le 23/05/2012 14:35, Steven Schveighoffer a écrit :
>>>> Yes. Memory allocation and deallocation from a global heap is by
>>>> definition an impure operation (it affects global state). However, we
>>>> must make exceptions because without being able to allocate memory,
>>>> pure
>>>> functions become quite trivial and useless.
>>>>
>>>> In functional languages, if such exceptions were not granted, a program
>>>> would not be able to do much of anything.
>>>>
>>>
>>> Yes, you are missing the point.
>>>
>>> collect is not something you should be able to call in a pure
>>> function. It can be triggered by allocating, but at this point you
>>> already are in an impure context called from a pure context.
>>>
>>> At the end, you need an unsafe way to call impure code in pure
>>> functions.
>>
>> I'm failing to see an argument in this response. If I can call an impure
>> function for allocating memory, why is it illegal to call an impure
>> function for collecting unused memory?
>>
>> -Steve
>
> Allocating is a much more simpler operation than collect, and its impact
> is way more reduced.
>
> Plus, allocating memory is something mandatory to do anything non
> trivial. GC collect isn't that important, as it can be triggered by
> allocating mecanism itself (and this mecanism is already impure).
>
> If you do allow everything impure to be done on pure function based on
> the fact that allocating is impure, what is the point to have pure
> function at all ?
You'll find a different between :
pure void foo() {
new Stuff(); // May collect
}
and
pure void foo() {
gc.collect();
}
The second one is obviously not pure. And the first one need a hook to
an allocating function in druntime, function which isn't pure, but made
look like a pure one, and that is able to call impure gc.collect .
gc.collect is a system wide procedure that involve all thread runniong
in your application and every single piece of memory in it. This is
probable the most far away from pure function I can think of.
More information about the Digitalmars-d
mailing list