Thread overview
Why std.array : array needs pure postblit?
Jun 15, 2018
Andrea Fontana
Jun 15, 2018
Basile B.
Jun 15, 2018
Basile B.
Jun 15, 2018
Andrea Fontana
Jun 15, 2018
Andrea Fontana
June 15, 2018
Check this code:
https://run.dlang.io/is/PoluHI

It won't work, because array appender requires a pure postblit.

Why? Can we remove this limitation?

Andrea
June 15, 2018
On Friday, 15 June 2018 at 11:15:03 UTC, Andrea Fontana wrote:
> Check this code:
> https://run.dlang.io/is/PoluHI
>
> It won't work, because array appender requires a pure postblit.
>
> Why? Can we remove this limitation?
>
> Andrea

Hello, i've tested locally and it can works by making `Appender.reserve()` and `Appender.ensureAddable()` function templates.

If you want to open an issue i'd be okay to propose a PR.
June 15, 2018
On Friday, 15 June 2018 at 11:24:42 UTC, Basile B. wrote:
> On Friday, 15 June 2018 at 11:15:03 UTC, Andrea Fontana wrote:
>> Check this code:
>> https://run.dlang.io/is/PoluHI
>>
>> It won't work, because array appender requires a pure postblit.
>>
>> Why? Can we remove this limitation?
>>
>> Andrea
>
> Hello, i've tested locally and it can works by making `Appender.reserve()` and `Appender.ensureAddable()` function templates.
>
> If you want to open an issue i'd be okay to propose a PR.

Patch is as simple as

---
@@ -2953,11 +2953,11 @@ if (isDynamicArray!A)
      * done.
      *
      * Params:
      *     newCapacity = the capacity the `Appender` should have
      */
-    void reserve(size_t newCapacity) @safe pure nothrow
+    void reserve()(size_t newCapacity)
     {
         if (_data)
         {
             if (newCapacity > _data.capacity)
                 ensureAddable(newCapacity - _data.arr.length);
@@ -2988,11 +2988,11 @@ if (isDynamicArray!A)
          */
         return cast(typeof(return))(_data ? _data.arr : null);
     }

     // ensure we can add nelems elements, resizing as necessary
-    private void ensureAddable(size_t nelems) @trusted pure nothrow
+    private void ensureAddable()(size_t nelems) @trusted
     {
         if (!_data)
             _data = new Data;
         immutable len = _data.arr.length;
         immutable reqlen = len + nelems;

---
June 15, 2018
On Friday, 15 June 2018 at 11:25:49 UTC, Basile B. wrote:

>> Hello, i've tested locally and it can works by making `Appender.reserve()` and `Appender.ensureAddable()` function templates.
>>

That was my idea too. But I wonder if pureness of reserve and ensureAddable have a particular reason.
June 15, 2018
On Friday, 15 June 2018 at 11:48:59 UTC, Andrea Fontana wrote:
> On Friday, 15 June 2018 at 11:25:49 UTC, Basile B. wrote:
>
>>> Hello, i've tested locally and it can works by making `Appender.reserve()` and `Appender.ensureAddable()` function templates.
>>>
>
> That was my idea too. But I wonder if pureness of reserve and ensureAddable have a particular reason.

My point is: if we remove pure from those functions it is inferred , isn't it? Why does we need to force it there?
June 15, 2018
On 6/15/18 7:56 AM, Andrea Fontana wrote:
> On Friday, 15 June 2018 at 11:48:59 UTC, Andrea Fontana wrote:
>> On Friday, 15 June 2018 at 11:25:49 UTC, Basile B. wrote:
>>
>>>> Hello, i've tested locally and it can works by making `Appender.reserve()` and `Appender.ensureAddable()` function templates.
>>>>
>>
>> That was my idea too. But I wonder if pureness of reserve and ensureAddable have a particular reason.
> 
> My point is: if we remove pure from those functions it is inferred , isn't it? Why does we need to force it there?

Best way to check is to look in git history to see why it was added.

https://github.com/dlang/phobos/pull/1337

Looks like the idea is to make pure and @safe appending work.

It's quite old (2013), so it's possible that attribute inference wasn't up to snuff back then, but works OK now.

Make sure you put in test cases that show pure @safe formatting still works after your changes.

-Steve