Creating immutable arrays in @safe code

ag0aep6g anonymous at example.com
Sun Jul 18 10:02:07 UTC 2021


On 17.07.21 15:56, ag0aep6g wrote:
> At a glance, the only meaningful use of `PURE.strong` seems to be in 
> dcast.d, introduced by the PR you linked. Changing that to `PURE.const_` 
> doesn't break any tests for me. So I'm inclined to believe that 
> `PURE.strong` is nonsense, and that `PURE.const_` already means 
> "strongly pure".
> 
> However, changing that instance doesn't fix the issue. Apparently, DMD 
> doesn't even recognize
> 
>      int[] array(const int[] input) pure { ... }
> 
> as `PURE.const_`.

I've dug a bit deeper, and apparently I'm to blame for confusing things. 
In issue 15862 [1], I stated that functions with mutable indirections in 
the return type cannot be strongly pure. That's wrong, but it seems to 
have found its way into DMD.

The core of issue 15862 is true: Two calls to `array` cannot be merged 
into one. But that doesn't make it weakly pure. Mutability in the return 
type is distinct from weak/strong purity.

These are all true:

* `array` is "strongly pure".
* `array` is a "pure factory function".
* The result of one call to `array` cannot be reused for another, 
identical call.


[1] https://issues.dlang.org/show_bug.cgi?id=15862.


More information about the Digitalmars-d-learn mailing list