Replies: 1 comment 3 replies
-
There is one level of implicit casting allowed in Mojo returns. I.E. if I have a struct that has an initializer with the signature struct Example:
fn __init__(inout self, data: Scalar[DType.float32]):
... If I then have a function that returns a Float32, it can be implicitly converted by the compiler, i.e. fn make_example(val: Float32) -> Example:
return val + 1 Will run as intended and return an Example struct. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a question related to a code snippet from the mojo docs: https://docs.modular.com/mojo/notebooks/Matmul. The code snippet is the one exactly after the text: "Adding types to the Python implementation" in the docs.
The code:
Here, getitem() returns Scalar[type] and fn load() returns SIMD[type, nelts]. These functions are connected, because getitem() calls fn load().
My question is the following: The data type, that gets returned from fn load() to getitem is a SIMD, which is I think a vector, containing 1 value that is DType.float32 . But after this, getitem() has to return a Scalar[type], which in this case is DType.float32. The same happens for setitem() and fn store().
So how can getitem() receive one data type and return another one? Is there an implicit conversion happening from a SIMD vector of a single element to a scalar? If so, could you please explain how this works?
Also is this for all the functions in mojo, or is it a dunder method or just a regular struct method thing? Or is it because in the current way of implementation, fn load() only returns a SIMD vector with only 1 element? So what would happen if the load function is used within getitem with nelts set to more than 1? It seems like it would return a SIMD vector, which would not be compatible with the expected return type of getitem.
Beta Was this translation helpful? Give feedback.
All reactions