Pure Factory Functions 💔 `inout`

Quirin Schroll qs.il.paperinik at gmail.com
Tue Feb 21 13:58:56 UTC 2023


On Tuesday, 21 February 2023 at 13:13:27 UTC, ag0aep6g wrote:
> On 21.02.23 11:46, Quirin Schroll wrote:
>> On Friday, 10 February 2023 at 14:37:35 UTC, ag0aep6g wrote:
> [...]
>>> That function "has no parameters with mutable indirections". 
>>> So it's strongly pure, not weakly pure.
>> 
>> Uniqueness analysis depends on the return type as much as on 
>> parameter types.
>
> I was only commenting on weakly/strongly pure. As you say, the 
> return type matters for pure factory functions. But it doesn't 
> matter for weakly/strongly pure.
>
> All pure factory functions are strongly pure. Not all strongly 
> pure functions are pure factory functions.

No, that is not true. The following `foo` is weakly pure, it 
mutates its input. But because the input cannot end up in the 
result (proven via their types, not the implementation), the 
result is unique, and can be converted to immutable.
```d
@safe:

int* foo(double[] xs) pure
{
     if (xs.length >= 2)
         xs[0] = xs[$ - 1];
     return new int;
}

void main()
{
     double[] xs = [];
     immutable a = foo(xs);
}
```

The whole section of pure factory functions should be revised (it 
could moved to a textbook like Ali’s) because it wants to talk 
about unique results, but it talks about a special case of it.


More information about the Digitalmars-d mailing list