Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-05 Thread Salih Dincer via Digitalmars-d-learn
On Thursday, 5 October 2023 at 16:40:49 UTC, Gaurav Negi wrote: Well, in the D programming language, both opIndex and opSlice are two different operators used to access elements of a custom type. Yeah, D is on its way to becoming a near-perfect programming language... ```d enum

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-05 Thread Gaurav Negi via Digitalmars-d-learn
Well, in the D programming language, both opIndex and opSlice are two different operators used to access elements of a custom type. struct S(T) { T[] arr; T opIndex(size_t index) const { assert(index < arr.length, "Index out of range");

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-03 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 3 October 2023 at 18:29:49 UTC, Salih Dincer wrote: More importantly, is there a priority order? Because in our last example, when we leave a single overload, all features are executed through the ref opIndex except the bit: The spec says: If an index expression can be rewritten

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-03 Thread Salih Dincer via Digitalmars-d-learn
On Tuesday, 3 October 2023 at 18:09:55 UTC, Imperatorn wrote: At the very least, the spec should do a better job of documenting when the compiler will try a fallback and when it won't. Who will be the hero and add the documentation?  More importantly, is there a priority order? Because

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-03 Thread Imperatorn via Digitalmars-d-learn
On Tuesday, 3 October 2023 at 17:52:20 UTC, Paul Backus wrote: On Tuesday, 3 October 2023 at 16:45:39 UTC, Steven Schveighoffer wrote: OK, so it's not as bad as I thought, but surely the compiler should recognize that `opIndexAssign(val, idx)` doesn't work, but `opIndex(idx) = val` does?

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-03 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 3 October 2023 at 16:45:39 UTC, Steven Schveighoffer wrote: OK, so it's not as bad as I thought, but surely the compiler should recognize that `opIndexAssign(val, idx)` doesn't work, but `opIndex(idx) = val` does? Maybe. On the other hand, if you make a typo in the body of your

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-03 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/3/23 12:09 PM, Paul Backus wrote: On Tuesday, 3 October 2023 at 13:07:00 UTC, Steven Schveighoffer wrote: Now, you can define a further `opIndexAssign(T val, size_t idx)`. However, now you lose capabilities like `a[0]++`, which I don't think has a possibility of implementing using an

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-03 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 3 October 2023 at 13:07:00 UTC, Steven Schveighoffer wrote: Now, you can define a further `opIndexAssign(T val, size_t idx)`. However, now you lose capabilities like `a[0]++`, which I don't think has a possibility of implementing using an `opIndex` operator, and it would be

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-03 Thread Steven Schveighoffer via Digitalmars-d-learn
On Monday, 2 October 2023 at 20:42:14 UTC, Paul Backus wrote: On Monday, 2 October 2023 at 20:34:11 UTC, Salih Dincer wrote: In an old version (for example, v2.0.83), the code you implemented in the places where Slice is written above works as desired. In the most current versions, the

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-02 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 2 October 2023 at 20:42:14 UTC, Paul Backus wrote: I don't know what's wrong in your example but this works for me: I found the reason for the error: https://forum.dlang.org/thread/vckvftkdzcrnikudu...@forum.dlang.org SDB@79

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-02 Thread Paul Backus via Digitalmars-d-learn
On Monday, 2 October 2023 at 20:34:11 UTC, Salih Dincer wrote: In an old version (for example, v2.0.83), the code you implemented in the places where Slice is written above works as desired. In the most current versions, the parameterized opIndexAssign(T value) gives the error:

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-02 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 2 October 2023 at 16:05:39 UTC, Paul Backus wrote: `T[] opSlice()` is the D1 version and exists only for backwards compatibility. You should use `T[] opIndex()` in new code. Forgive me for asking again, I think opSliceAssign(T value) has also been improved, right? ```d //

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-02 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 1 October 2023 at 17:41:08 UTC, Salih Dincer wrote: Hi, What is the difference between T[] opIndex() and T[] opSlice(), which haven't parameters? `T[] opSlice()` is the D1 version and exists only for backwards compatibility. You should use `T[] opIndex()` in new code.

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-02 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 2 October 2023 at 02:01:34 UTC, Jonathan M Davis wrote: For most code, you'd just write an opIndex with a single parameter for indexing an element, opSlice with two parameters for slicing the range or container, and then either opIndex or opSlice with no parameters to return a

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-01 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/1/23 1:41 PM, Salih Dincer wrote: Hi, What is the difference between T[] opIndex() and T[] opSlice(), which haven't parameters? None. It used to be that opSlice was the only way, and the mechanisms opSlice uses are still valid. -Steve

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-01 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, October 1, 2023 11:51:17 AM MDT Salih Dincer via Digitalmars-d- learn wrote: > On Sunday, 1 October 2023 at 17:41:08 UTC, Salih Dincer wrote: > > Also, is it correct to use [] when returning? > > > > Thanks... > > [Here](https://dlang.org/spec/operatoroverloading.html#slice), > the

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-01 Thread Salih Dincer via Digitalmars-d-learn
On Sunday, 1 October 2023 at 17:41:08 UTC, Salih Dincer wrote: Also, is it correct to use [] when returning? Thanks... [Here](https://dlang.org/spec/operatoroverloading.html#slice), the opIndex() is proposed and an example of parameterized the opSlice() is given for multidimensional

The difference between T[] opIndex() and T[] opSlice()

2023-10-01 Thread Salih Dincer via Digitalmars-d-learn
Hi, What is the difference between T[] opIndex() and T[] opSlice(), which haven't parameters? ```d struct S(T) {    T[] arr;        T[] opIndex() => arr[];/*    T[] opSlice() => arr;//*/ } alias Type = int; void main() {    auto s = S!Type([1,2,3]);    auto arr = s[]; //