Open SleepyRoy opened 3 years ago
Hey, I would like to deal with this issue but I am confused about how to do it.
TL;DR : how to implement an operator overload between .vtl.Tensor<T>
(note the . at the beginning) objects ?
I naively tried to implement the + overload like this :
//+ operator overload
[inline]
pub fn (a &Tensor<f64>) + (b &Tensor<f64>) &Tensor<f64>{
return add(a, b)
}
getting this error on my test :
fn test_plus_operator(){
a := ones<int>([2,2])
b := ones<int>([2,2])
assert a + b == add(a, b)
---- Testing... ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
FAIL 327.520 ms /home/cmnemoi/Documents/Code/V/vtl/math_op_test.v
math_op_test.v:7:9: error: invalid operator `+` to `&.Tensor<int>` and `&.Tensor<int>`
5 | b := ones<int>([2,2])
6 |
7 | assert a + b == add(a, b)
| ~~~~~
8 | }
After some experimentation with the V doc example on operator overloading, I guessed overloading is not supported between object references (is it ?).
Then I implemented the overload on Tensors copies :
[inline]
pub fn (a Tensor<f64>) + (b Tensor<f64>) &Tensor<f64>{
return add(a, b)
}
and get the following error :
fn test_plus_operator(){
a := ones<int>([2,2])
b := ones<int>([2,2])
assert *a + *b == add(a, b) //dereferencing vtl.Tensors
---- Testing... ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
FAIL 327.520 ms /home/cmnemoi/Documents/Code/V/vtl/math_op_test.v
math_op_test.v:7:9: error: invalid operator `+` to `.Tensor<int>` and `.Tensor<int>`
5 | b := ones<int>([2,2])
6 |
7 | assert *a + *b == add(a, b) //dereferencing vtl.Tensors
| ~~~~~
8 | }
What is a .Tensor
On another side, when I try to use the + operator outside vtl
files I don't deal with .&vtl.Tensor
but actual &vtl.Tensor
.
I am very confused.
@cmnemoi hey! thanks for your contributions! unfortunately I think V is not capable to overload operators on structs that use generics for now :/
.Tensor
is nothing 😅 probably an issue in V throwing errors 👀
That makes sense, thanks for the answer.
(I guess V's op overload is now stable enough so...)
VTL version: master
OS: win10
What did you do? (Just take plus op as an example)
What did you expect to see? a 2*2 matrix with elements 2.0
What did you see instead?