www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to call a templated overload of opIndex

reply Marc <mailnawman gmail.com> writes:
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
next sibling parent reply Nick Treleaven <nick geany.org> writes:
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
parent Marc <mailnawman gmail.com> writes:
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:
 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).
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)`?
Aug 15
prev sibling parent reply =?UTF-8?Q?Ali_=C3=87ehreli?= <acehreli yahoo.com> writes:
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
parent Marc <mailnawman gmail.com> writes:
On Friday, 15 August 2025 at 21:09:50 UTC, Ali Çehreli wrote:
 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
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; ```
Aug 15