# Convert an StaticArray to a Tuple

**URL:** <https://forum.crystal-lang.org/t/convert-an-staticarray-to-a-tuple/5852>\
**Category:** Help & Support\
**Created:** [July 20, 2023, 8:54am UTC](https://forum.crystal-lang.org/t/convert-an-staticarray-to-a-tuple/5852 "2023-07-20T08:54:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![doongjohn](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@doongjohn](https://forum.crystal-lang.org/u/doongjohn)\
**Post date:** [July 20, 2023, 8:54am UTC](https://forum.crystal-lang.org/t/convert-an-staticarray-to-a-tuple/5852/1 "2023-07-20T08:54:15Z")

</div>

I understand `Array` cannot be converted to `Tuple` without specifing the type because `Array` size is runtime known.

But why is converting `StaticArray` to `Tuple` not supported?

```auto
Tuple.from(StaticArray[1, 2])

```

```plaintext
expected argument #1 to 'Tuple(*T).from' to be Array(T), not StaticArray(Int32, 2)
                                         Overloads are:
                                          - Tuple(*T).from(array : Array)

```

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [July 20, 2023, 4:33pm UTC](https://forum.crystal-lang.org/t/convert-an-staticarray-to-a-tuple/5852/2 "2023-07-20T16:33:05Z")

</div>

May I ask what’s the use case? I can’t imagine why you would need this.

You can do something like `StaticArray[1, 2].unsafe_as(Tuple(Int32, Int32))` because the memory representation is the same.

---

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [July 20, 2023, 8:43pm UTC](https://forum.crystal-lang.org/t/convert-an-staticarray-to-a-tuple/5852/3 "2023-07-20T20:43:42Z")

</div>

I suspect it wasn’t a conscious choice not to support it, but I bet it’s just that nobody has had a need for it. Most things in the stdlib exist because someone needed it, but `StaticArray` is pretty rare. I’ve only ever really seen it used as a micro-optimization, such as the buffer for a `Slice` to avoid allocating on the heap inside a completely encapsulated operation where the `StaticArray` and the `Slice` never leave the method. It seems either you have a very uncommon use case or a different solution may be more appropriate for what you’re trying to accomplish.

That said, if you really need this, you can make it work pretty simply with this patch:

```crystal
struct StaticArray
  def to_tuple
    {% begin %}
      {
        {% for index in 0...N %}
          self[{{index}}],
        {% end %}
      }
    {% end %}
  end
end

```

---

<div class="post-metadata">

**Author:** ![doongjohn](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@doongjohn](https://forum.crystal-lang.org/u/doongjohn)\
**Post date:** [July 23, 2023, 8:56am UTC](https://forum.crystal-lang.org/t/convert-an-staticarray-to-a-tuple/5852/4 "2023-07-23T08:56:10Z")

</div>

Just curious, why is StaticArray rarely used? Is fixed size array not very useful in crystal?

---

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [July 23, 2023, 6:02pm UTC](https://forum.crystal-lang.org/t/convert-an-staticarray-to-a-tuple/5852/5 "2023-07-23T18:02:24Z")

</div>

For most things, a `Slice` fills that role. `StaticArray` doesn’t incur GC cost, but `Slice` has a few benefits over the `StaticArray`:

- It doesn’t require the size to be known at compile time because the size is part of the type
- Mutating a `StaticArray` can cause subtle bugs because the buffer is allocated on the stack, whereas `Slice`’s buffer is typically a reference to heap-allocated memory

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [July 24, 2023, 7:36am UTC](https://forum.crystal-lang.org/t/convert-an-staticarray-to-a-tuple/5852/6 "2023-07-24T07:36:40Z")

</div>

My question was not insinuating that `StaticArray` is rarely used. I was asking about the conversion from `StaticArray` to `Tuple` for which I cannot think of many use cases.

---

<div class="post-metadata">

**Author:** ![doongjohn](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@doongjohn](https://forum.crystal-lang.org/u/doongjohn)\
**Post date:** [July 27, 2023, 10:40am UTC](https://forum.crystal-lang.org/t/convert-an-staticarray-to-a-tuple/5852/7 "2023-07-27T10:40:10Z")

</div>

I was reading about `Splats and tuples` and I thought it would be cool to splat StaticArray. But splat was only for the tuple type. (I’m learning the language and I really like it 😊)

I just wanted to know if this is possible:

```auto
def some_fn(a, b, c) end
some_fn *StaticArray[1, 2, 3].to_tuple

```

and now I know it is possible thanks to @straight-shoota and @jgaskins
