{"action":"created","issue":{"url":"https://api.github.com/repos/manastech/crystal/issues/1984","labels_url":"https://api.github.com/repos/manastech/crystal/issues/1984/labels{/name}","comments_url":"https://api.github.com/repos/manastech/crystal/issues/1984/comments","events_url":"https://api.github.com/repos/manastech/crystal/issues/1984/events","html_url":"https://github.com/manastech/crystal/issues/1984","id":124274751,"number":1984,"title":"How to add a hook to inherited classes","user":{"login":"veelenga","id":3624712,"avatar_url":"https://avatars.githubusercontent.com/u/3624712?v=3","gravatar_id":"","url":"https://api.github.com/users/veelenga","html_url":"https://github.com/veelenga","followers_url":"https://api.github.com/users/veelenga/followers","following_url":"https://api.github.com/users/veelenga/following{/other_user}","gists_url":"https://api.github.com/users/veelenga/gists{/gist_id}","starred_url":"https://api.github.com/users/veelenga/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/veelenga/subscriptions","organizations_url":"https://api.github.com/users/veelenga/orgs","repos_url":"https://api.github.com/users/veelenga/repos","events_url":"https://api.github.com/users/veelenga/events{/privacy}","received_events_url":"https://api.github.com/users/veelenga/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-12-29T22:04:39Z","updated_at":"2015-12-30T12:57:51Z","closed_at":null,"body":"I want to add a `method_added` hook to all subclasses. My try:\r\n\r\n```crystal\r\nclass A\r\n  macro inherited\r\n    macro method_added(m)\r\n      # ...\r\n    end\r\n  end\r\nend\r\n\r\nclass B < A\r\n  def foo; end\r\nend\r\n```\r\nAnd it works, but  I am not able to expand `m` with `{{m}}` because it is not a variable of the outer macro.\r\n\r\nPlease advise a right way to implement that."},"comment":{"url":"https://api.github.com/repos/manastech/crystal/issues/comments/167992251","html_url":"https://github.com/manastech/crystal/issues/1984#issuecomment-167992251","issue_url":"https://api.github.com/repos/manastech/crystal/issues/1984","id":167992251,"user":{"login":"asterite","id":209371,"avatar_url":"https://avatars.githubusercontent.com/u/209371?v=3","gravatar_id":"","url":"https://api.github.com/users/asterite","html_url":"https://github.com/asterite","followers_url":"https://api.github.com/users/asterite/followers","following_url":"https://api.github.com/users/asterite/following{/other_user}","gists_url":"https://api.github.com/users/asterite/gists{/gist_id}","starred_url":"https://api.github.com/users/asterite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/asterite/subscriptions","organizations_url":"https://api.github.com/users/asterite/orgs","repos_url":"https://api.github.com/users/asterite/repos","events_url":"https://api.github.com/users/asterite/events{/privacy}","received_events_url":"https://api.github.com/users/asterite/received_events","type":"User","site_admin":false},"created_at":"2015-12-30T12:57:51Z","updated_at":"2015-12-30T12:57:51Z","body":"You need to escape the inner macro's `{{` and `{%`. For example:\r\n\r\n```\r\nclass A\r\n  macro inherited\r\n    macro method_added(m)\r\n      \\{{ puts m }}\r\n    end\r\n  end\r\nend\r\n\r\nclass B < A\r\n  def foo; end\r\nend\r\n```\r\n\r\nOtherwise, if you do `{{ puts m }}`, the macro will try to expand it as soon as possible. With the `\\{{`, the first macro will ignore it but generate `{{ puts m }}` as its output, and then the second macro will expand it.\r\n\r\nKind of confusing, but that's how to deal with nested macros :-)"}}