Re: Flatten a range of static arrays

2020-02-08 Thread Steven Schveighoffer via Digitalmars-d-learn
On 2/8/20 1:20 PM, ag0aep6g wrote: On 08.02.20 15:57, Steven Schveighoffer wrote: This kind of stuff is so difficult to reason about and develop as a library that people will just end up removing dip1000 from their compilation. I 100% agree that DIP 1000 is hard to reason about. It's pretty l

Re: Flatten a range of static arrays

2020-02-08 Thread ag0aep6g via Digitalmars-d-learn
On 08.02.20 07:27, ag0aep6g wrote: On 08.02.20 02:38, ag0aep6g wrote: Simplified, we're looking at this: struct Joiner { int[3] _items; int[] _current; } void main() @safe { Joiner j; j._current = j._items[]; } [...] In the first reduction, `j` might be `scope`, b

Re: Flatten a range of static arrays

2020-02-08 Thread ag0aep6g via Digitalmars-d-learn
On 08.02.20 15:57, Steven Schveighoffer wrote: This kind of stuff is so difficult to reason about and develop as a library that people will just end up removing dip1000 from their compilation. I 100% agree that DIP 1000 is hard to reason about. It's pretty limited by design, and the implement

Re: Flatten a range of static arrays

2020-02-08 Thread Steven Schveighoffer via Digitalmars-d-learn
On 2/7/20 8:38 PM, ag0aep6g wrote: If that code were allowed, you could do this: struct Joiner {     Joiner* p; } Joiner g; void main() @safe {     scope Joiner j;     () @trusted { j.p = &j; } (); /* pretend it's allowed */     g = *j.p; /* dereference and copy */ } Returning a

Re: Flatten a range of static arrays

2020-02-07 Thread ag0aep6g via Digitalmars-d-learn
On 08.02.20 02:38, ag0aep6g wrote: Simplified, we're looking at this: struct Joiner {     int[3] _items;     int[] _current; } void main() @safe {     Joiner j;     j._current = j._items[]; } I.e., a self-referential struct. Or most fundamentally: struct Joiner {     Joiner

Re: Flatten a range of static arrays

2020-02-07 Thread ag0aep6g via Digitalmars-d-learn
On 08.02.20 01:17, Steven Schveighoffer wrote: The original code is not invalid though. f is not valid, and that is a bug, but the original code posted by nullptr should be fine by memory safety standards. Maybe. But then it should also work with `int[3] front;`. If there is no possible way t

Re: Flatten a range of static arrays

2020-02-07 Thread Steven Schveighoffer via Digitalmars-d-learn
On 2/7/20 6:30 PM, ag0aep6g wrote: On 08.02.20 00:10, nullptr wrote: ``` import std; struct SomeRange { int[3] val; enum empty = false; auto popFront() @safe {} ref auto front() @safe { return val; } } void main() @safe { SomeRange().take(10).map!

Re: Flatten a range of static arrays

2020-02-07 Thread Steven Schveighoffer via Digitalmars-d-learn
On 2/7/20 6:10 PM, nullptr wrote: ``` import std; struct SomeRange {     int[3] val;     enum empty = false;     auto popFront() @safe {}     ref auto front() @safe     {     return val;     } } void main() @safe {     SomeRange().take(10).map!((return ref x) => x[]).joiner.writ

Re: Flatten a range of static arrays

2020-02-07 Thread ag0aep6g via Digitalmars-d-learn
On 08.02.20 00:10, nullptr wrote: ``` import std; struct SomeRange {     int[3] val;     enum empty = false;     auto popFront() @safe {}     ref auto front() @safe     {     return val;     } } void main() @safe {     SomeRange().take(10).map!((return ref x) => x[]).joiner.write

Re: Flatten a range of static arrays

2020-02-07 Thread nullptr via Digitalmars-d-learn
On Friday, 7 February 2020 at 22:55:29 UTC, Dennis wrote: Oops, minimized a bit too much. Corrected test case: ``` import std; struct S { @safe: int[3] front = [10, 20, 30]; bool empty = false; void popFront() {empty = true;} } void main() @safe { S.init.map!((return ref x)

Re: Flatten a range of static arrays

2020-02-07 Thread Dennis via Digitalmars-d-learn
On Friday, 7 February 2020 at 21:40:36 UTC, Steven Schveighoffer wrote: S.popFront is not @safe, and S is not a template. So no inferrence. Oops, minimized a bit too much. Corrected test case: ``` import std; struct S { @safe: int[3] front = [10, 20, 30]; bool empty = false; voi

Re: Flatten a range of static arrays

2020-02-07 Thread Steven Schveighoffer via Digitalmars-d-learn
On 2/7/20 4:17 PM, Dennis wrote: On Friday, 7 February 2020 at 20:55:14 UTC, nullptr wrote: Depending on how your range is structured, it might be possible to just mark front as returning by ref to make this work. That's a good one. I can't make front() return by ref, but I can make front a m

Re: Flatten a range of static arrays

2020-02-07 Thread Dennis via Digitalmars-d-learn
On Friday, 7 February 2020 at 20:55:14 UTC, nullptr wrote: Depending on how your range is structured, it might be possible to just mark front as returning by ref to make this work. That's a good one. I can't make front() return by ref, but I can make front a member variable of the range struct

Re: Flatten a range of static arrays

2020-02-07 Thread Dennis via Digitalmars-d-learn
On Friday, 7 February 2020 at 20:31:47 UTC, Steven Schveighoffer wrote: The only solution I can provide is to wrap the static array into a range (maybe something like this exists in Phobos?): Thanks. I was hoping something like that existed in Phobos, but I can't find anything.

Re: Flatten a range of static arrays

2020-02-07 Thread nullptr via Digitalmars-d-learn
On Friday, 7 February 2020 at 20:13:57 UTC, Dennis wrote: If I have an input range with element type `int[3]`, how do I easily turn it into a range of `int` so I can map it? If it were an int[3][] I could simply cast it to an int[] before mapping, but I don't want to eagerly turn it into an arr

Re: Flatten a range of static arrays

2020-02-07 Thread Steven Schveighoffer via Digitalmars-d-learn
On 2/7/20 3:13 PM, Dennis wrote: If I have an input range with element type `int[3]`, how do I easily turn it into a range of `int` so I can map it? If it were an int[3][] I could simply cast it to an int[] before mapping, but I don't want to eagerly turn it into an array. I thought of doing th

Flatten a range of static arrays

2020-02-07 Thread Dennis via Digitalmars-d-learn
If I have an input range with element type `int[3]`, how do I easily turn it into a range of `int` so I can map it? If it were an int[3][] I could simply cast it to an int[] before mapping, but I don't want to eagerly turn it into an array. I thought of doing this: ``` range.map!(x => x[]).joine