Skip to content

Instantly share code, notes, and snippets.

@craigp
Forked from KronicDeth/deprecation.md
Created May 23, 2017 14:38
Show Gist options
  • Save craigp/80b4603ecd53df4f8a8472aa210a51d6 to your computer and use it in GitHub Desktop.
Save craigp/80b4603ecd53df4f8a8472aa210a51d6 to your computer and use it in GitHub Desktop.
HowTo make deprecation warnings in Elixir

TIL how to make deprecation warnings:

Compiling 2 files (.ex)
warning: `Retort.Resources.timeout/2` is deprecated; call `Retort.Resources.Timeout.get_or_default/2` instead.
  lib/retort/resources.ex:197: Retort.Resources.timeout/2

Replace function

  @spec timeout(module) :: Keyword.t | timeout
  defp timeout(module) do
    :retort
    |> Application.get_env(module, [])
    |> Keyword.get(:timeout, @default_timeout)
  end

With (undocumented) macro that replaces the call

  @doc false
  @spec timeout(module, Retort.Resources.Timeout.function_name) :: timeout
  defmacro timeout(module, function_name) do
    IO.warn "`Retort.Resources.timeout/2` is deprecated; call `Retort.Resources.Timeout.get_or_default/2` instead.",
            Macro.Env.stacktrace(__ENV__)

    quote do
      Retort.Resources.Timeout.get_or_default(unquote(module), unquote(function_name))
    end
  end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment