# Macro Help Determining Type

**URL:** <https://forum.crystal-lang.org/t/macro-help-determining-type/5495>\
**Category:** Help & Support\
**Created:** [March 21, 2023, 11:17pm UTC](https://forum.crystal-lang.org/t/macro-help-determining-type/5495 "2023-03-21T23:17:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eric](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@eric](https://forum.crystal-lang.org/u/eric)\
**Post date:** [March 21, 2023, 11:17pm UTC](https://forum.crystal-lang.org/t/macro-help-determining-type/5495/1 "2023-03-21T23:17:19Z")

</div>

I have a hash which stores a few different object types but with the way the application is structured, given a key it will always return a certain object type. That key may be missing through. I want to wrap it with accessors that simplify that type juggling.

Here is my macro:

```auto
private macro section_getter(name, type=String)
  def {{name.id}}
    ret = @data[{{name.stringify.capitalize}}]?.try &.as {{type}}

    {% if type.stringify.starts_with? "Array" %}
      ret ||= [] of {{type.type_vars.first}}
    {% end %}

    ret
  end
end

```

The idea is to call it with something like:

```crystal
section_getter foo
section_getter bar, Array(Item)
section_getter baz

```

The idea here is that if I call the `foo` method I get back either a `String` or `nil`. Same with `baz`. But `bar` is different. It always returns an array although it might be an empty array. Basically if type is not an array then the method return an object of that type or nil. If the type is an array then it always returns an array but that array may be empty if the underlying hash didn’t have that key.

What I don’t really love is the if statement in the macro. I feel like I should have:

```crystal
{% if type.is_a? Array %}

```

But that doesn’t work. I’ve tried various other things and none of them work so my hacky convert it to a string and see if it starts with `Array` is the best I can do. It works but it seems like there should be a better way.

---

<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:** [March 21, 2023, 11:27pm UTC](https://forum.crystal-lang.org/t/macro-help-determining-type/5495/2 "2023-03-21T23:27:15Z")

</div>

Try `type <= Array`.

---

<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:** [March 21, 2023, 11:32pm UTC](https://forum.crystal-lang.org/t/macro-help-determining-type/5495/3 "2023-03-21T23:32:54Z")

</div>

Check [How to append to an Array that is a default Value of a Hash? - #5 by bcardiff](https://forum.crystal-lang.org/t/how-to-append-to-an-array-that-is-a-default-value-of-a-hash/2144/5) and the referenced github issue for what I think is a related thing you are trying to do. :-)
