# Int autocast when visit a hash key is a Int or give compile error instead?

**URL:** <https://forum.crystal-lang.org/t/int-autocast-when-visit-a-hash-key-is-a-int-or-give-compile-error-instead/8055>\
**Category:** Help & Support\
**Created:** [May 13, 2025, 2:37pm UTC](https://forum.crystal-lang.org/t/int-autocast-when-visit-a-hash-key-is-a-int-or-give-compile-error-instead/8055 "2025-05-13T14:37:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [May 13, 2025, 2:37pm UTC](https://forum.crystal-lang.org/t/int-autocast-when-visit-a-hash-key-is-a-int-or-give-compile-error-instead/8055/1 "2025-05-13T14:37:17Z")

</div>

```crystal
map = Hash(Int32, String).new { |hash, k| hash[k] = "#{k} " }

p map[13.to_i64]

```

Above code build no error, but will get runtime error.

```auto
Unhandled exception: Missing hash key: 13 (KeyError)
  from /home/zw963/Crystal/share/crystal/src/hash.cr:498 in '[]'
  from 1.cr:14:3 in '__crystal_main'
  from /home/zw963/Crystal/share/crystal/src/crystal/main.cr:129:5 in 'main_user_code'
  from /home/zw963/Crystal/share/crystal/src/crystal/main.cr:115:7 in 'main'
  from /home/zw963/Crystal/share/crystal/src/crystal/system/unix/main.cr:9:3 in 'main'
  from /usr/lib/libc.so.6 in '??'
  from /usr/lib/libc.so.6 in '__libc_start_main'
  from /home/zw963/.cache/crystal/crystal-run-1.tmp in '_start'
  from ???

```

I thought, can we give a compile-time error instead, or just autocast no any error?

Thanks

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [May 13, 2025, 2:44pm UTC](https://forum.crystal-lang.org/t/int-autocast-when-visit-a-hash-key-is-a-int-or-give-compile-error-instead/8055/2 "2025-05-13T14:44:15Z")

</div>

Related: [Better handling of Hash with default block but of the wrong type · Issue #15057 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/issues/15057)

Is another issue referenced in this issue about adding type restrictions to `Hash` to make it more comp time safe, but it’s a breaking change so it’s kinda in limbo.
