Saving sufficient information about array to construct a similar one later

I have a function f which takes in x::AbstractArray. My function returns another function g (a closure). Inside g, I do not need x, but I need to construct similar(x).

Therefore, so that I do not to waste memory by closing over the entire array, I would not like g to close over x, but just close over sufficient information so that I can construct the equivalent of similar(x) inside g. Does the AbstractArray interface support this (fairly well-motivated) use case?

I think this is solved? Close over:

T = typeof(x)
ax = axes(x)

and run

similar(T, ax)

.

2 Likes

Note: due to the issue noted in Closures over types are type unstable, one must capture T using a where, rather than using typeof. The aforementioned issue addresses the case where this is not possible – I don’t have a solution in this case yet.