# Can't access hash value based on type in macro

**URL:** <https://forum.crystal-lang.org/t/cant-access-hash-value-based-on-type-in-macro/1304>\
**Category:** Help & Support\
**Created:** [October 30, 2019, 12:08pm UTC](https://forum.crystal-lang.org/t/cant-access-hash-value-based-on-type-in-macro/1304 "2019-10-30T12:08:44Z")\
**Posts on this page:** 6\
**Page:** 1

<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:** [October 30, 2019, 12:08pm UTC](https://forum.crystal-lang.org/t/cant-access-hash-value-based-on-type-in-macro/1304/1 "2019-10-30T12:08:44Z")

</div>

```auto
class Foo
end
 
class Bar
end
 
CONST = {
  Int32 => Foo,
  Int64 => Bar
}
 
 
{{pp CONST[Int32]}} # => nil

```

[https://play.crystal-lang.org/#/r/7wz2](https://play.crystal-lang.org/#/r/7wz2)

I also tried wrapping it in `macro finished`. Using `String` keys works fine, just not like `Int32.class`. Am I missing something?

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [October 30, 2019, 12:25pm UTC](https://forum.crystal-lang.org/t/cant-access-hash-value-based-on-type-in-macro/1304/2 "2019-10-30T12:25:24Z")

</div>

I think in the CONST definition `Int32` is just a `Path` node, but in the macro call it’s a `TypeNode`, and they are not equal. We could make try to make them equal, or resolve hash keys but it’s a bit complex… why do you need this?

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [October 30, 2019, 12:30pm UTC](https://forum.crystal-lang.org/t/cant-access-hash-value-based-on-type-in-macro/1304/3 "2019-10-30T12:30:25Z")

</div>

For example this works:

```auto
class Foo
end

class Bar
end

CONST = {Int32 => 1}

macro path(path)
  {{puts CONST[path]}}
end

path(Int32)

```

The reason is that `Int32` is passed as a `Path` node to the macro, and it remains a `Path` unless you `resolve` it. But in macros a `Path` is immediately resolved.

We could maybe add a `path` macro method to get a `Path`, for example doing `{{ path(Int32) }}` would get you `Int32` as a `Path` node.

But macros are already pretty complex. As I always say, I would try to avoid them at all cost, except for simple uses.

---

<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:** [October 30, 2019, 1:11pm UTC](https://forum.crystal-lang.org/t/cant-access-hash-value-based-on-type-in-macro/1304/4 "2019-10-30T13:11:42Z")

</div>

Ah that makes sense. I was playing around with some refactoring in Granite where I would map a Crystal type to a record representing a database type. like `{Int32 => Integer, Int64 => BigInteger}` that internally would handle how to convert the value to/from the database. I could easily enough just use `String` keys, was just curious if there was something else going on.

---

<div class="post-metadata">

**Author:** ![bew](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bew/32/113_2.png) [@bew](https://forum.crystal-lang.org/u/bew)\
**Post date:** [October 30, 2019, 5:17pm UTC](https://forum.crystal-lang.org/t/cant-access-hash-value-based-on-type-in-macro/1304/5 "2019-10-30T17:17:33Z")

</div>

Having a `TypeNode#path` would help in this case, do i open an issue? (or someone else, i don’t have much more right now)

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [October 30, 2019, 5:33pm UTC](https://forum.crystal-lang.org/t/cant-access-hash-value-based-on-type-in-macro/1304/6 "2019-10-30T17:33:48Z")

</div>

`TypeNode#path` is not good because if you type `Bar` it might resolve to `Foo::Bar` and then the path will be `Foo::Bar`, not `Bar`. In fact, matching on `Path` might not be good because of this reason, so I don’t know.
