Replies: 7 comments 5 replies
-
You would have to make an alias for the int slice. type Slice = []int
(a1 Slice) + (a2 Slice) Slice {
result := Slice([]int{len: a1.len, init: 0})
for i in 0 .. a1.len{
result[i] = a1[i] + a2[i]
}
return result
} |
Beta Was this translation helpful? Give feedback.
-
neither of the above code snippets works.There is something missing. |
Beta Was this translation helpful? Give feedback.
-
You can overload a struct containing a generic [], e.g.
|
Beta Was this translation helpful? Give feedback.
-
Just tried to understand why there is also no overload for Vec2[T] and scalar multiplication, but only overload for element-wise multiplication:
adding the following in vlib/math/vec/vec2.v
results unfortunately in
Would be great to have the feature of Vec2, Vec3, and Vec4 * scalar overload. |
Beta Was this translation helpful? Give feedback.
-
It exists not as overload but as a method: // multiply_scalar multiplies the vector with `scalar`.
pub fn (mut v Vec2[T]) multiply_scalar[U](scalar U) {
v.x *= T(scalar)
v.y *= T(scalar)
} https://github.com/vlang/v/blob/master/vlib/math/vec/vec2.v#L159C1-L164C1 |
Beta Was this translation helpful? Give feedback.
-
I know I have seen the method pub fn (v Vec2[T]) * (scalar U) Vec2[T] { to see whether one can extend the vec2.v with a simple scalar overload. I understand that:
Given that
and I also don't think that this would lead to confusion that 5 * vec.Vec2 would give 5 copies. Line 137 in 951d304 |
Beta Was this translation helpful? Give feedback.
-
Besides shadowing, V requires both types of operator overloading be the same. Lets you say you want to sustract an integer from a nibble. We get these errors: struct Nibble { n u8 }
struct Integer { n u8 }
fn (n Nibble) + (m Nibble) Nibble {
return Nibble { n: n.n + m.n }
}
// expected `Nibble` not `Integer` - both operands must be the same type for operator overloading
fn (n Nibble) - (i Integer) Nibble {
return Nibble { n: n.n - i.n }
}
fn main() {
println('${Nibble{1} + Nibble{3}}') // 4 ok
//println('${Nibble{3} - Integer{1}}') // mismatched types `Nibble` and `Integer`
} |
Beta Was this translation helpful? Give feedback.
-
I am very interested as a scientist to use the V Language because of the simplicity and ease of writing while still generating highly optimizable code that can run on everything through the use of C as an IR.
There is just one thing that I havent quite been able to figure out. The Documentation mentions that Operator overloading exists mainly for the Science Domains that should be supported. So I was trying to use operator overloading to create a simple way to do Vector Addition (Element Wise array addition). I know I could just make a loop instead but thats just in general less readable.
So far i tried to do it as follows:
But this does not seem to work is there a way to do this if anyone knows something good here it would be greatly appreciated.
Beta Was this translation helpful? Give feedback.
All reactions