Pure functions in D
Denis Koroskin
2korden at gmail.com
Mon Sep 22 15:28:18 PDT 2008
On Tue, 23 Sep 2008 02:12:58 +0400, Steven Schveighoffer
<schveiguy at yahoo.com> wrote:
> "bearophile" wrote
>> Walter:
>>> Parameters to a pure function can be mutable, but calls cannot be
>>> cached
>>> or executed asynchronously if the arguments contain any references to
>>> mutable data.<
>>
>> I don't understand how can such function be pure.
>
> It can't be. I think Walter made a mistake there. Otherwise, you could
> pass data that another thread changes while in the middle of using it.
>
> Maybe he meant that a pure function can *return* mutable data? Then
> 'new'
> could be considered pure?
>
> Another possibility is that Walter is assuming the shared/unshared
> paradigm
> is in place.
>
> Another possibility is that pure functions that accept mutable parameters
> can only be called from other pure functions, therefore you are
> guaranteed
> that the data is not being used in any other thread.
>
> Who knows what he was thinking ;) But the current article statement is
> definitely wrong without clarification.
>
> -Steve
>
>
I think he meant both:
> Pure functions do not require synchronization for use by multiple
> threads,
> because their data is all either *thread local* or immutable.
Given that an implicit unshared attribute is attached to mutable variable
arguments such functions are definitely pure:
pure void inplace_tolower(char[] str); // yes!
pure Foo bar()
{
char[] str = "HELLO".dup;
inplace_tolower(str);
...
}
More information about the Digitalmars-d
mailing list