Skip to Main Content
Mastering Elixir
book

Mastering Elixir

by André Albuquerque, Daniel Caixinha
July 2018
Intermediate to advanced content levelIntermediate to advanced
574 pages
14h 7m
English
Packt Publishing
Content preview from Mastering Elixir

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Testing Elixir

Testing Elixir

Andrea Leopardi, Jeffrey Matthias

Publisher Resources

ISBN: 9781788472678Supplemental Content