# Calling macro incorrectly

**URL:** <https://forum.crystal-lang.org/t/calling-macro-incorrectly/1208>\
**Category:** Help & Support\
**Created:** [October 8, 2019, 8:04pm UTC](https://forum.crystal-lang.org/t/calling-macro-incorrectly/1208 "2019-10-08T20:04:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwoertink](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jwoertink/32/50_2.png) [@jwoertink](https://forum.crystal-lang.org/u/jwoertink)\
**Post date:** [October 8, 2019, 8:04pm UTC](https://forum.crystal-lang.org/t/calling-macro-incorrectly/1208/1 "2019-10-08T20:04:05Z")

</div>

I don’t really need help with this, but more wanted to just present this issue.

If you define a macro that is looking for a type declaration, then you call that macro and forget to pass the type\_declaration.var, then nothing will happen. This is because of how crystal allows you to define variables in the open space.

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

```crystal
abstract class One
  macro foo_bar(type_dec)
    {% raise type_dec.var %}
  end
end

class Two < One
  # I expected this to call the macro, but it doesn't
  foo_bar : String
end

```

In this case, you could call the macro with parenthesis `foo_bar( : String)`. This would raise an error (albeit not the error you’d want). My guess is this isn’t something that could really be fixed, and just a side affect to how crystal works. But I figured I would post this in case it sparks any ideas

---

<div class="post-metadata">

**Author:** ![girng\_github](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/girng_github/32/571_2.png) [@girng\_github](https://forum.crystal-lang.org/u/girng_github)\
**Post date:** [October 9, 2019, 2:16pm UTC](https://forum.crystal-lang.org/t/calling-macro-incorrectly/1208/2 "2019-10-09T14:16:52Z")

</div>

cc @Blacksmoke16

---

<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 9, 2019, 2:37pm UTC](https://forum.crystal-lang.org/t/calling-macro-incorrectly/1208/3 "2019-10-09T14:37:32Z")

</div>

I’m not sure I see the issue here?

- `foo_bar : String` - This doesn’t call the macro since it thinks you’re just typing a variable called `foo_bar`

- `foo_bar( : String)` - The paren tells it you’re doing a method/macro call, but correctly errors since the `TypeDeclaration` you passed it doesn’t have a `var` defined. I.e it’s incomplete.

- `foo_bar(name : String)` - Errors with what you want correctly since the paren tells it you want to call the macro and you’re passing it a complete `TypeDeclaration`.

---

<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 9, 2019, 2:48pm UTC](https://forum.crystal-lang.org/t/calling-macro-incorrectly/1208/4 "2019-10-09T14:48:02Z")

</div>

Indeed.

Maybe we could give an error if you do `var : Type` but then never assign to `var`. That way you’ll know you might have used `var` incorrectly. But I don’t know how easy is that (it should be, but you never know).

---

<div class="post-metadata">

**Author:** ![jwoertink](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jwoertink/32/50_2.png) [@jwoertink](https://forum.crystal-lang.org/u/jwoertink)\
**Post date:** [October 9, 2019, 4:14pm UTC](https://forum.crystal-lang.org/t/calling-macro-incorrectly/1208/5 "2019-10-09T16:14:13Z")

</div>

Yeah, not really an issue. This came up from someone working on a lucky app that did

```crystal
belongs_to : Owner

```

but forgot to add the type declaration var. The app still compiled, and was throwing a different error. My initial thought was that it shouldn’t have compiled, but that was because I didn’t know you could type a local variable like that.

It would be cool to catch something like that, but I also know that it’s not really a huge issue, so if it doesn’t happen then it’s no worries. 🙂

---

<div class="post-metadata">

**Author:** ![vlazar](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/vlazar/32/31_2.png) [@vlazar](https://forum.crystal-lang.org/u/vlazar)\
**Post date:** [October 9, 2019, 5:26pm UTC](https://forum.crystal-lang.org/t/calling-macro-incorrectly/1208/6 "2019-10-09T17:26:20Z")

</div>

`belongs_to : Owner` reads great btw :)
