29 lines
585 B
V
29 lines
585 B
V
pub type FnArrayInit = fn (idx int) T
|
|
|
|
pub fn new_array<T>(len int, initfn FnArrayInit<T>) []T {
|
|
mut res := []T{len: len}
|
|
for idx in 0 .. res.len {
|
|
res[idx] = initfn(idx)
|
|
}
|
|
return res
|
|
}
|
|
|
|
struct Point3D {
|
|
x f32
|
|
y f32
|
|
z f32
|
|
}
|
|
|
|
fn main() {
|
|
// this works as expected:
|
|
good_cloud := new_array<Point3D>(3, fn (idx int) Point3D {
|
|
return Point3D{idx, idx * 10, idx * -10}
|
|
})
|
|
println(good_cloud)
|
|
// this should be a compilation error, because the function signature does not match:
|
|
bad_cloud := new_array<Point3D>(3, fn (idx int) f64 {
|
|
return 12345
|
|
})
|
|
println(bad_cloud)
|
|
}
|