See the following functions. dbl2 works with SIMD inputs, but dbl does not compile: error: expected a type, not a value.
How can I implement this generic dbl without using SIMD types?
fn dbl[T: DType](x: T) -> T:
return 2*x
fn dbl2[T: DType](x: Scalar[T]) -> Scalar[T]:
return 2*x
fn main():
var x = 4
var y = dbl(x)
print(y)
var x2 = SIMD[DType.float32, 1](4)
var y2 = dbl2(x2)
print(y2)
Numerical values in Mojo are SIMD based that means DType by its own needs to be wrapped in SIMD. The Scalar[T] is SIMD[T, 1]where 1 is the SIMD width. For convenience there’re SIMD aliases for each DType like Float32 which corresponds to DType.float32.
If we do var x = 4, the type of x is Int. The dbl2(x) does not work in this case. However, if I do var x = Int32(4), dbl2(x) works. Intbeing used by default and not being one of the Int32, Int16 etc looks strange to me. It can create inconsistencies, and the example here is one such case.