M
Modular7mo ago
Firas

RayTracing example - __add__ method (dunder methods)

In the RayTracing example, in the first class implementation Vec3f, in the dunder methods (add sub ...etc) the implementation is like this: fn add(self, other: Vec3f) -> Vec3f: return self.data + other.data this return data, and it's type is SIMD, shouldn't it be: fn add(self, other: Vec3f) -> Vec3f: return Vec3f(self.data + other.data) ?? I tried both and both compiled and worked. But why the the first implementation (the one in the example) work!! the function needs to return Vec3f but it returns SIMD !!
3 Replies
benny
benny7mo ago
it looks like the function IS returning Vec3f, the reason the first method works is an implict cast to Vec3f. Essentially, since Vec3f contains an __init__ method with the signature fn __init__(data: SIMD[DType.float32, 4]) -> Self:, if you return a SIMD[DType.float32, 4] from a function that is intended to return Vec3f, Mojo can implictly cast between the types.
Firas
FirasOP7mo ago
Ok wow thanks a lot !!
benny
benny7mo ago
ofc 👍🏻 the only sharp edge is if you need to cast to another type first, it can only do one implicit cast
Want results from more Discord servers?
Add your server