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

Salih Dincer salihdb at hotmail.com
Sun Oct 1 17:41:08 UTC 2023


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[]; // calls s.opIndex()
     
     assert(arr == [1,2,3]);
     assert(is(typeof(arr): Type[]));
}
```

Also, is it correct to use [] when returning?

Thanks...


More information about the Digitalmars-d-learn mailing list