# Call callback-function inside Behaviour-Module

**URL:** <https://forum.elixirforum.com/t/call-callback-function-inside-behaviour-module/67469>\
**Category:** Questions\
**Tags:** behaviours, callbacks\
**Created:** [November 13, 2024, 2:45pm UTC](https://forum.elixirforum.com/t/call-callback-function-inside-behaviour-module/67469 "2024-11-13T14:45:05Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![al2o3cr](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/al2o3cr/32/3457_2.png) [@al2o3cr](https://forum.elixirforum.com/u/al2o3cr)\
**Post date:** [November 13, 2024, 7:30pm UTC](https://forum.elixirforum.com/t/call-callback-function-inside-behaviour-module/67469/5 "2024-11-13T19:30:21Z")

</div>

Another option would be to _generate_ all the required code into the module that says `use`. This replaces threading `callback_module` everywhere by local calls:

```elixir
defmodule MyBehaviour do
  @callback specific_handling(data :: term) :: term

  @callback very_specific_handling(data :: term) :: term

  defmacro __using__ (_opts) do
    quote do
      @behaviour MyBehaviour

      alias MyBehaviour

      def initiate_processing(data) do
        data
        |> do_stuff_with_data()
        # and other calls to behaviour-functions
        |> specific_handling()
      end

      defp do_stuff_with_data(data) do
        data
        # and other calls to behaviour-functions
        |> do_specific_stuff()
      end

      defp do_specific_stuff(data) do
        data
        # and other calls to behaviour-functions
        |> very_specific_handling()
      end
    end
  end
end

defmodule DataHandler11 do
  use MyBehaviour

  def specific_handling(data) do
    # do specific stuff with data
    data
  end

  def very_specific_handling(data) do
    # do specific stuff with data
    data
  end
end

defmodule DataHandler12 do
  use MyBehaviour

  def specific_handling(data) do
    # do specific stuff with data
    data
  end

  def very_specific_handling(data) do
    # do specific stuff with data
    data
  end
end

data = nil
handler = 1

case handler do
  1 -> DataHandler11.initiate_processing(data)
  2 -> DataHandler12.initiate_processing(data)
end

```

This has a cost: the code output by ` __using__ ` is compiled over again for every module that calls it, versus only being compiled once when `MyBehaviour` is compiled.

---

_[View the full topic](https://forum.elixirforum.com/t/call-callback-function-inside-behaviour-module/67469)._
