Re: The Phobos Put

2023-03-31 Thread Paul Backus via Digitalmars-d-learn
On Friday, 31 March 2023 at 02:23:29 UTC, Steven Schveighoffer wrote: There's a certain attempt in phobos in some places to try and ensure code that is going to confuse will not compile. I think this is one of those attempts. Consider that if you pass a slice into `put`, then it returns

Re: The Phobos Put

2023-03-30 Thread Steven Schveighoffer via Digitalmars-d-learn
On 3/30/23 11:44 AM, Paul Backus wrote: It should be fine to have both a `ref` and non-`ref` overload for `put`, though, right? If the non-`ref` overload is only called with rvalues, then it's fine to leave them in an undetermined state, because nothing can access them afterward anyway.

Re: The Phobos Put

2023-03-30 Thread Salih Dincer via Digitalmars-d-learn
On Thursday, 30 March 2023 at 13:27:33 UTC, Steven Schveighoffer wrote: But you can do `dig.copy(buf[])` since a dynamic array is. Apparently, we will not be able to get rid of the necessity of using slices.  Neither with "copy" nor with "put"... ```d import std.algorithm.mutation : copy;

Re: The Phobos Put

2023-03-30 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 29 March 2023 at 20:50:04 UTC, Steven Schveighoffer wrote: On 3/29/23 4:29 PM, ag0aep6g wrote: But regardless of Salih's exact intent, the broader point is: a non-ref overload could be added to Phobos. And that would enable `a[1..$-1].phobos_put([2, 3])`. Which is what he asked

Re: The Phobos Put

2023-03-30 Thread Steven Schveighoffer via Digitalmars-d-learn
On 3/29/23 11:01 PM, Salih Dincer wrote: ```d import std.algorithm.mutation : copy; void main() {   int[8] buf;   auto dig = [1, 2, 3, 4];   auto rem = dig.copy(buf);   assert(rem.length == 4); } ``` Looks like 'copy' has the same overload issue. A static array is not a range of any

Re: The Phobos Put

2023-03-29 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 29 March 2023 at 20:50:04 UTC, Steven Schveighoffer wrote: On 3/29/23 4:29 PM, ag0aep6g wrote: But regardless of Salih's exact intent, the broader point is: a non-ref overload could be added to Phobos. And that would enable `a[1..$-1].phobos_put([2, 3])`. Which is what he asked

Re: The Phobos Put

2023-03-29 Thread Steven Schveighoffer via Digitalmars-d-learn
On 3/29/23 4:29 PM, ag0aep6g wrote: But regardless of Salih's exact intent, the broader point is: a non-ref overload could be added to Phobos. And that would enable `a[1..$-1].phobos_put([2, 3])`. Which is what he asked about originally. I think the idea of requiring ref output ranges is

Re: The Phobos Put

2023-03-29 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 29 March 2023 at 20:29:24 UTC, ag0aep6g wrote: But regardless of Salih's exact intent, the broader point is: a non-ref overload could be added to Phobos. And that would enable `a[1..$-1].phobos_put([2, 3])`. Which is what he asked about originally. Yes, that was it, but even

Re: The Phobos Put

2023-03-29 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 29 March 2023 at 19:49:47 UTC, Ali Çehreli wrote: On 3/29/23 12:21, ag0aep6g wrote: > As far as I understand, you're saying that we cannot overload on `ref`. > But we can. Salih's code demonstrates just that. > > void f(ref int x) {} > void f(int x) {} > void main() { int x; f(x);

Re: The Phobos Put

2023-03-29 Thread ag0aep6g via Digitalmars-d-learn
On Wednesday, 29 March 2023 at 19:49:47 UTC, Ali Çehreli wrote: I thought Salih was proposing two more overloads to the existing put(). When I copy the existing put(), which takes 'ref R', not R[], then the code does not compile: auto put(R)(R[] range, R[] source) => putImpl(range, source);

Re: The Phobos Put

2023-03-29 Thread Ali Çehreli via Digitalmars-d-learn
On 3/29/23 12:21, ag0aep6g wrote: > As far as I understand, you're saying that we cannot overload on `ref`. > But we can. Salih's code demonstrates just that. > > void f(ref int x) {} > void f(int x) {} > void main() { int x; f(x); f(42); } /* no errors */ I thought Salih was proposing two more

Re: The Phobos Put

2023-03-29 Thread ag0aep6g via Digitalmars-d-learn
t(R)(ref R[] range, R[] source) >=> putImpl(range, source); That's for lvalues. If you are proposing keeping the current ref-taking Phobos put() as well, then the following call would be ambiguous: slice.put([2]); // Compilation ERROR As far as I understand, you're saying

Re: The Phobos Put

2023-03-29 Thread Ali Çehreli via Digitalmars-d-learn
ce); That's for lvalues. If you are proposing keeping the current ref-taking Phobos put() as well, then the following call would be ambiguous: slice.put([2]); // Compilation ERROR That can be remedied by using template constraints for 'slices' vs. other output ranges. But that would b

Re: The Phobos Put

2023-03-29 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 29 March 2023 at 15:01:27 UTC, Ali Çehreli wrote: On 3/29/23 04:48, Dennis wrote: On the other hand, Phobos's put() works with any OutputRange so it has to take a 'ref' to advance to know where it is left off. This behavior makes its use with slices weird but sometimes such is

Re: The Phobos Put

2023-03-29 Thread Ali Çehreli via Digitalmars-d-learn
On 3/29/23 04:48, Dennis wrote: > On Wednesday, 29 March 2023 at 11:10:42 UTC, Salih Dincer wrote: >> Why does my `put` work but the Phobos `put` doesn't work with a slice? > > Your `put` doesn't take `range` by `ref`, so it allows you to pass an > rvalue. Consequently,

Re: The Phobos Put

2023-03-29 Thread Dennis via Digitalmars-d-learn
On Wednesday, 29 March 2023 at 11:10:42 UTC, Salih Dincer wrote: Why does my `put` work but the Phobos `put` doesn't work with a slice? Your `put` doesn't take `range` by `ref`, so it allows you to pass an rvalue. Consequently, it doesn't advance the range from the callers perspective.

The Phobos Put

2023-03-29 Thread Salih Dincer via Digitalmars-d-learn
Why does my `put` work but the Phobos `put` doesn't work with a slice? onlineapp.d(11): Error: none of the overloads of template `std.range.primitives.put` are callable using argument types `!()(int[], int[])` /dlang/dmd/linux/bin64/../../src/phobos/std/range/primitives.d(386

The Phobos put()

2023-03-29 Thread Salih Dincer via Digitalmars-d-learn
Why does my `put` work but the Phobos `put` doesn't work with a slice? onlineapp.d(11): Error: none of the overloads of template `std.range.primitives.put` are callable using argument types `!()(int[], int[])` /dlang/dmd/linux/bin64/../../src/phobos/std/range/primitives.d(386