Macros: Hooks

Hooks

Special macros exist that are invoked in some situations as hooks: inherited, included, extended and method_missing.

  • inherited is invoked at compile-time when a subclass is defined. @type is the inheriting type.
  • included is invoked at compile-time when a module is included. @type is the including type.
  • extended is invoked at compile-time when a module is extended. @type is the extending type.
  • method_missing is invoked at compile-time when a method is not found.

Example of inherited:

class Parent
  macro inherited
    def lineage
      "{{@type.name.id}} < Parent"
    end
  end
end

class Child < Parent
end

Child.new.lineage #=> "Child < Parent"

Example of method_missing:

macro method_missing(call)
  print "Got ", {{call.name.id.stringify}}, " with ", {{call.args.size}}, " arguments", '\n'
end

foo          # Prints: Got foo with 0 arguments
bar 'a', 'b' # Prints: Got bar with 2 arguments

To the extent possible under law, the persons who contributed to this workhave waived
all copyright and related or neighboring rights to this workby associating CC0 with it.
https://crystal-lang.org/docs/syntax_and_semantics/macros/hooks.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部