# Destructing named tuple?

**URL:** <https://forum.crystal-lang.org/t/destructing-named-tuple/1524>\
**Category:** Community\
**Created:** [December 23, 2019, 3:37pm UTC](https://forum.crystal-lang.org/t/destructing-named-tuple/1524 "2019-12-23T15:37:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexeypetrushin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/alexeypetrushin/32/492_2.png) [@alexeypetrushin](https://forum.crystal-lang.org/u/alexeypetrushin)\
**Post date:** [December 23, 2019, 3:37pm UTC](https://forum.crystal-lang.org/t/destructing-named-tuple/1524/1 "2019-12-23T15:37:25Z")

</div>

Is there a way to destruct named tuple?

```
prices_daily = [
  { year: 2000, month: 1, day: 1, price: 1 },
  { year: 2000, month: 1, day: 2, price: 2 },

  { year: 2000, month: 2, day: 1, price: 3 },
]
grouped_by_month = prices_daily.group_by do |tuple| 
  { tuple[:year], tuple[:month] }
end

```

I was trying to use something like code below, but it wont’ compile

```
grouped_by_month = prices_daily.group_by do | year, month |
  { year, month }
end
```

---

<div class="post-metadata">

**Author:** ![bcardiff](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bcardiff/32/3_2.png) [@bcardiff](https://forum.crystal-lang.org/u/bcardiff)\
**Post date:** [December 23, 2019, 6:20pm UTC](https://forum.crystal-lang.org/t/destructing-named-tuple/1524/2 "2019-12-23T18:20:35Z")

</div>

Unpacking works for tuples (but not named tuples). Ref: [https://crystal-lang.org/reference/syntax\_and\_semantics/blocks\_and\_procs.html#unpacking-block-arguments](https://crystal-lang.org/reference/syntax_and_semantics/blocks_and_procs.html#unpacking-block-arguments)

Even if you create a macro macro `proj(t, :year, :month)` that will transform the code to `{ t[:year], t[:month} }` it wont be much different to the first code:

```auto
prices_daily.group_by { |t| proj(t, :year, :month) }
prices_daily.group_by { |t| { t[:year], t[:month] } }

```

Note that there is no way a method in a named tuple would be able to preserve the typing from the named tuple components to a tuple, so that why I mention a macro,.

---

<div class="post-metadata">

**Author:** ![alexeypetrushin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/alexeypetrushin/32/492_2.png) [@alexeypetrushin](https://forum.crystal-lang.org/u/alexeypetrushin)\
**Post date:** [December 23, 2019, 6:47pm UTC](https://forum.crystal-lang.org/t/destructing-named-tuple/1524/3 "2019-12-23T18:47:20Z")

</div>

Thanks, any plans to extend it for named tuples too in the future? **In theory** it looks reasonable

NamedTuple

```auto
array = [{number: 1, word: "one"}, {number: 2, word: "two"}]
array.each do |{number, word}|
  puts "#{number}: #{word}"
end

```

Tuple

```auto
array = [{1, "one"}, {2, "two"}]
array.each do |(number, word)|
  puts "#{number}: #{word}"
end

```

---

<div class="post-metadata">

**Author:** ![bcardiff](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bcardiff/32/3_2.png) [@bcardiff](https://forum.crystal-lang.org/u/bcardiff)\
**Post date:** [December 23, 2019, 7:03pm UTC](https://forum.crystal-lang.org/t/destructing-named-tuple/1524/4 "2019-12-23T19:03:09Z")

</div>

A limitation with named tuples is that `{A: 1}` is a valid named tuple, but `A` is not a valid local variable.

And something like the following is a bit less fun probably.

```auto
array = [{Number: 1, Word: "one"}, {Number: 2, Word: "two"}]
array.each do |{Number: number, Word: word}|
  puts "#{number}: #{word}"
end

```

No plans AFAIK
