# Blog post: \`reveal\_type\` in Crystal

**URL:** <https://forum.crystal-lang.org/t/blog-post-reveal-type-in-crystal/5463>\
**Category:** Official\
**Tags:** blog\
**Created:** [March 6, 2023, 9:33pm UTC](https://forum.crystal-lang.org/t/blog-post-reveal-type-in-crystal/5463 "2023-03-06T21:33:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![beta-ziliani](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/beta-ziliani/32/1821_2.png) [@beta-ziliani](https://forum.crystal-lang.org/u/beta-ziliani)\
**Post date:** [March 6, 2023, 9:33pm UTC](https://forum.crystal-lang.org/t/blog-post-reveal-type-in-crystal/5463/1 "2023-03-06T21:33:40Z")

</div>

Brian (@bcardiff) wrote a blog post about a handy way to reveal the type of an expression. You’ll learn not only a neat tool, but also a bit of macro magic 🧙

> **[Reveal type in Crystal](https://crystal-lang.org/2023/03/06/reveal-type-in-crystal/)**
>
> Recently I came across reveal\_type from Sorbet as a way to inspect the type of an expression, thanks Brian Hicks. I wondered if that can be ported to Crystal. You can jump to the conclusions section if you want to copy-paste the good-enough™️...

---

<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:** [March 7, 2023, 7:52am UTC](https://forum.crystal-lang.org/t/blog-post-reveal-type-in-crystal/5463/2 "2023-03-07T07:52:55Z")

</div>

Following solution can be a supplement to above solution.

## 1. Add a `BlackHole` type for a universal type for any unknown type.

```auto
class BlackHole
  macro method_missing(call)
    raise "BUG: must implement {{call.name}}"
  end
end

```

## 2. for any places compiler complain “can not infer current type”, just use BlackHole as type.

## 3. compile again, compiler will tell you, “should be a String, not BlackHole”, then, you known, the correct type should be `String`.

I use this hack all the days when i porting Ruby gems into Crystal, this solution come from asterite by [this reply](https://forum.crystal-lang.org/t/is-there-exists-a-dart-dynamic-type-equivalent-for-crystal-or-equivalent-usage/5082/8)

---

<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:** [March 7, 2023, 8:54am UTC](https://forum.crystal-lang.org/t/blog-post-reveal-type-in-crystal/5463/3 "2023-03-07T08:54:55Z")

</div>

BTW, the code in blog conclusion not works on my laptop 1.7.2

```crystal
# 1.cr

def reveal_type_helper(t : T, l) : T forall T
  {% puts " : #{T}" %}
  t
end

macro reveal_type(t)
  {% loc = "#{t.filename.id}:#{t.line_number}:#{t.column_number}" %}
  {% puts "Revealed type #{loc.id}" %}
  {% puts " #{t.id}" %}
  reveal_type_helper({{t}}, { {{loc.tr("/:.", "___").id}}: 1 })
end

x = "hello"

reveal_type x

```

```auto
Revealed type /home/zw963/1.cr:15:13
  x
There was a problem expanding macro 'reveal_type'

Code in src/1.cr:15:1

 15 | reveal_type x
      ^
Called macro defined in src/1.cr:6:1

 6 | macro reveal_type(t)

Which expanded to:

   2 |   
   3 |   
 > 4 | reveal_type_helper(x, { _home_zw963_Crystal_crystal-china_ferrum_src_1_cr_15_13: 1 })
                                                                                        ^
Warning: space required before colon in type declaration (run `crystal tool format` to fix this)

A total of 1 warnings were found.
There was a problem expanding macro 'reveal_type'

Code in src/1.cr:15:1

 15 | reveal_type x
      ^
Called macro defined in src/1.cr:6:1

 6 | macro reveal_type(t)

Which expanded to:

 > 2 |   
 > 3 |   
 > 4 | reveal_type_helper(x, { _home_zw963_Crystal_crystal-china_ferrum_src_1_cr_15_13: 1 })
                                                                                          ^
Error: unexpected token: "1"

```

---

<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:** [March 7, 2023, 11:03am UTC](https://forum.crystal-lang.org/t/blog-post-reveal-type-in-crystal/5463/4 "2023-03-07T11:03:01Z")

</div>

The error happens because your path contains a dash (`-`) and the example code does not escape that.  
It needs more complete sanitizing to produce only valid symbol names.  
This particular issue can be fixed with using `loc.tr("/:.-", " ____")`. But that’s just a minor improvement and it’s still susceptible to other problems.  
Wrapping the name in quotes (and escaping `"`) would probably be a good idea.
