# Saving sufficient information about array to construct a similar one later

**URL:** <https://discourse.julialang.org/t/saving-sufficient-information-about-array-to-construct-a-similar-one-later/82331>\
**Category:** General Usage\
**Tags:** question, array\
**Created:** [June 6, 2022, 6:14pm UTC](https://discourse.julialang.org/t/saving-sufficient-information-about-array-to-construct-a-similar-one-later/82331 "2022-06-06T18:14:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tictaccat](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@tictaccat](https://discourse.julialang.org/u/tictaccat)\
**Post date:** [June 6, 2022, 6:14pm UTC](https://discourse.julialang.org/t/saving-sufficient-information-about-array-to-construct-a-similar-one-later/82331/1 "2022-06-06T18:14:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tictaccat](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@tictaccat](https://discourse.julialang.org/u/tictaccat)\
**Post date:** [June 6, 2022, 6:20pm UTC](https://discourse.julialang.org/t/saving-sufficient-information-about-array-to-construct-a-similar-one-later/82331/2 "2022-06-06T18:20:15Z")

</div>

I think this is solved? Close over:

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

```

and run

```julia
similar(T, ax)

```

.

---

<div class="post-metadata">

**Author:** ![tictaccat](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@tictaccat](https://discourse.julialang.org/u/tictaccat)\
**Post date:** [June 6, 2022, 6:45pm UTC](https://discourse.julialang.org/t/saving-sufficient-information-about-array-to-construct-a-similar-one-later/82331/3 "2022-06-06T18:45:47Z")

</div>

Note: due to the issue noted in [Closures over types are type unstable](https://discourse.julialang.org/t/closures-over-types-are-type-unstable/82337), 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.
