PR #43 introduced iterator of NDArray. Due to ASAP destruction of the variables, the array is destroyed after the last use, meaning that the user to manually call it after the iterator to extend its lifetime. Otherwise, there is a dangling pointer.
var A = nm.NDArray(10, random=True)
for i in A:
print(i, end="\t") # dangling pointer! Unexpected printing results.
var _ = A^ # Force A's lifetime to be extended to here.
This is now fixed by using built-in AnyLifetime type to explicitly indicate the lifetime of the pointer.
Test code:
import numojo as nm
fn main() raises:
test[nm.i8](10)
test[nm.f64](20)
fn test[dtype: DType](length: Int) raises:
var A = nm.NDArray[dtype](length, random=True)
print(A)
print("Iterate over the array:")
for i in A:
print(i, end="\t")
print()
print(str("=") * 30)
PR #43 introduced iterator of NDArray. Due to ASAP destruction of the variables, the array is destroyed after the last use, meaning that the user to manually call it after the iterator to extend its lifetime. Otherwise, there is a dangling pointer.
This is now fixed by using built-in
AnyLifetime
type to explicitly indicate the lifetime of the pointer.Test code:
Test results: