Using the use and __using__/1 macros

Our objective is now to choose the time unit that will be used to time our functions. To achieve this we will employ the use macro, which you have probably seen a few times now (the CacheWorker module previously implemented by us used  GenServer). Instead of importing the ElixirDrip.Chronometer module, we are now using it:

$ cat examples/measured_module.exsdefmodule MeasuredModule do  use ElixirDrip.Chronometer, unit: :secs  defchrono slow_times(x, y) do    Process.sleep(2000)    x * y  end  defchrono slow_square(x \\ 3) do    Process.sleep(2000)    x * x  endend

When the compiler finds the use statement on the MeasuredModule, it will run the __using__/1 macro of the ElixirDrip.Chronometer module. You will see this strategy ...

Get Mastering Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.