Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

operator overload doesn't work on Tensors #6

Open
SleepyRoy opened this issue May 27, 2021 · 3 comments
Open

operator overload doesn't work on Tensors #6

SleepyRoy opened this issue May 27, 2021 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@SleepyRoy
Copy link
Member

SleepyRoy commented May 27, 2021

(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)

import vtl

fn main() {
	println(vtl.ones([2,2])+vtl.ones([2,2]))
}

What did you expect to see?
a 2*2 matrix with elements 2.0

What did you see instead?

sample.v:4:14: error: undefined operation `vtl.Tensor` + `vtl.Tensor`
    2 |
    3 | fn main() {
    4 |     println(vtl.ones([2,2])+vtl.ones([2,2]))
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    5 | }
@ulises-jeremias ulises-jeremias added the enhancement New feature or request label Jun 4, 2021
@ulises-jeremias ulises-jeremias self-assigned this Jun 4, 2021
@cmnemoi
Copy link
Contributor

cmnemoi commented Jun 26, 2022

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 ? I couldn't find any reference in V ecosystem.

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.

@ulises-jeremias
Copy link
Member

@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 👀

@cmnemoi
Copy link
Contributor

cmnemoi commented Jun 27, 2022

That makes sense, thanks for the answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants