Store mutable indirections in immutable data with this one weird trick!
Paul Backus
snarwin at gmail.com
Sat Nov 13 12:42:59 UTC 2021
On Saturday, 13 November 2021 at 09:06:23 UTC, tsbockman wrote:
> On Saturday, 13 November 2021 at 08:32:42 UTC, Timon Gehr wrote:
>> On 13.11.21 09:30, tsbockman wrote:
>>>>
>>>
>>> That's a compiler bug.
>>
>> Nonsense. Results of strongly pure function calls implicitly
>> convert to immutable. Your "fix" just avoided purity inference.
>
> I see. This does not appear to be documented anywhere in the
> spec at the moment.
The conversion itself is not documented, but the spec does
explain why the original code is not allowed:
> A pure factory function is a strongly pure function that
> returns a result that has mutable indirections. All mutable
> memory returned by the call may not be referenced by any other
> part of the program, i.e. it is newly allocated by the
> function. Nor may the mutable references of the result refer to
> any object that existed before the function call.
So, having a strongly-pure function return a mutable pointer to
an existing object is forbidden.
I guess this is what we get for trying to make memory allocation
count as "pure."
More information about the Digitalmars-d
mailing list