www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why std.array : array needs pure postblit?

reply Andrea Fontana <nospam example.com> writes:
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
Jun 15 2018
parent reply Basile B. <b2.temp.temp.temp gmx.gmx.gmx.com.com> writes:
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.
Jun 15 2018
parent reply Basile B. <b2.temp.temp.temp gmx.gmx.gmx.com.com> writes:
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; ---
Jun 15 2018
parent reply Andrea Fontana <nospam example.com> writes:
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.
Jun 15 2018
parent reply Andrea Fontana <nospam example.com> writes:
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?
Jun 15 2018
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Jun 15 2018