digitalmars.D.learn - How to call a templated overload of opIndex
- Marc (17/17) Aug 15 Hello,
- Nick Treleaven (3/7) Aug 15 You need to write `opIndex!double(i, j)` to manually call the
- Marc (4/11) Aug 15 Thanks for your reply. It works but it their a way to call it
- =?UTF-8?Q?Ali_=C3=87ehreli?= (17/34) Aug 15 Calling it that way feels counter to operator overloading: It's supposed...
- Marc (10/18) Aug 15 Thanks a lot for your reply. It should be a bug to call it with
Hello, In my typed data frames I've this type of code. It works for default value (float) but I can't call `opIndex!double[i, j]` or `opIndex!string[i,j]`. It just doesn't work and I get the error `is not a template declaration, it is a variable`. ```d // Overload [i, j] ref T opIndex(T = float)(size_t i, size_t j) const { alias SliceType = Slice!(T*); // colValues is of type void*[], i cast it to a slice type and get // the value at index i (row index) auto values = cast(SliceType*) colValues[j]; return (*values)[i]; } ``` Any help would be really appreciated.
Aug 15
On Friday, 15 August 2025 at 17:38:53 UTC, Marc wrote:Hello, In my typed data frames I've this type of code. It works for default value (float) but I can't call `opIndex!double[i, j]` or `opIndex!string[i,j]`.You need to write `opIndex!double(i, j)` to manually call the method. (You're using square brackets).
Aug 15
On Friday, 15 August 2025 at 17:51:50 UTC, Nick Treleaven wrote:On Friday, 15 August 2025 at 17:38:53 UTC, Marc wrote:Thanks for your reply. It works but it their a way to call it using square brackets something like `df!float[i, j]` instead of calling for example `df.opIndex!float(i,j)`?Hello, In my typed data frames I've this type of code. It works for default value (float) but I can't call `opIndex!double[i, j]` or `opIndex!string[i,j]`.You need to write `opIndex!double(i, j)` to manually call the method. (You're using square brackets).
Aug 15
On 8/15/25 10:38 AM, Marc wrote:Hello, In my typed data frames I've this type of code. It works for default value (float) but I can't call `opIndex!double[i, j]` or `opIndex! string[i,j]`.Calling it that way feels counter to operator overloading: It's supposed to allow the [i, j] syntax. Additionally, it is not clear what the meaning of !double vs. !float is. Wouldn't it be a bug to call !double when the data is float?It just doesn't work and I get the error `is not a template declaration, it is a variable`. ```d // Overload [i, j] ref T opIndex(T = float)(size_t i, size_t j) const {Related to my concern above, returning a ref to double would be wrong when the data is float.alias SliceType = Slice!(T*); // colValues is of type void*[], i cast it to a slice type and get // the value at index i (row index) auto values = cast(SliceType*) colValues[j];Perhaps my concern in not valid because perhaps that cast is producing a new container of e.g. doubles. But then it would be inefficient to convert all elements to double. Further, returning a reference to an element of a newly created container doesn't feel useful.return (*values)[i]; } ``` Any help would be really appreciated.This design doesn't seem useful. How about something like the following: import std.conv : to; x[i, j].to!double; y[i, j].to!float; Ali
Aug 15
On Friday, 15 August 2025 at 21:09:50 UTC, Ali Çehreli wrote:Thanks a lot for your reply. It should be a bug to call it with double when the data is float and vice versa. I managed to make it work with a variant / sumtype. Now i can do the following ```d auto df = new DataFrame!Ts(...) auto value = df[0, 0]; //or float value = df[0,0].get!float; ```Any help would be really appreciated.This design doesn't seem useful. How about something like the following: import std.conv : to; x[i, j].to!double; y[i, j].to!float; Ali
Aug 15