a := [4]u32{1, 1, 1, 1}
b := [4]f64(a) // This doesn't work
c := [4]f64{f64(a.x), f64(a.y), f64(a.z), f64(a.w)} // This does
This can be tedious for larger arrays. Should I just iterate over the array and cast individually into a separate array? That’s what I’ve been doing and it’s not a problem. I’m just curious what the alternatives are.
// Casts array A to type T
acast :: proc(array: [$L]$A, $T: typeid) -> [L]T {
res := [L]T {}
#no_bounds_check for i in 0..<L {
res[i] = cast(T) array[i]
}
return res
}
main :: proc() {
a := [4]u32 { 5, 33, 2, 6}
b := acast(a, f32)
}
I feel like this is already in a Core package somewhere…
core:math/linalg has an array_cast proc for this, and also some similar procs for specific target types (e.g. to_f32). This is effectively the same thing as acast above.