# Define a function at compile-time

**URL:** <https://forum.elixirforum.com/t/define-a-function-at-compile-time/70123>\
**Category:** Questions\
**Tags:** metaprogramming\
**Created:** [March 25, 2025, 7:56am UTC](https://forum.elixirforum.com/t/define-a-function-at-compile-time/70123 "2025-03-25T07:56:35Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![lkuty](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lkuty/32/34879_2.png) [@lkuty](https://forum.elixirforum.com/u/lkuty)\
**Post date:** [March 25, 2025, 8:42am UTC](https://forum.elixirforum.com/t/define-a-function-at-compile-time/70123/5 "2025-03-25T08:42:15Z")

</div>

> [@LostKobrakai](#):
>
> You can use `@on_definition` on a module (say `A`) to have it call a macro or function elsewhere (Say on `B`) whenever a function or macro is defined on the module. With a macro being provided you can then pull information from those definitions and store them in an arbitrary module attribute of `A`.

I’ve put `@on_definition {Hooks, :rule_ids}` in module `Rules` but in the other module called `Hooks` I cannot see how I can accumulate the ids and make use of them in the `rule_ids/6` function.

```elixir
defmodule Hooks do
  Module.register_attribute( __MODULE__ , :rule_ids, accumulate: true, persist: false)

  def rule_ids(_env, :def, :condition, [rule_id | _], _guards, _body)
  when is_integer(rule_id)
  do
    IO.puts rule_id
  end
  def rule_ids(_env, _kind, _name, _args, _guards, _body), do: nil
end

```

---

_[View the full topic](https://forum.elixirforum.com/t/define-a-function-at-compile-time/70123)._
