Skip to content

Instantly share code, notes, and snippets.

@sb8244
Created April 23, 2020 18:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sb8244/74dcf03e472560ca80bffaf94a73ade1 to your computer and use it in GitHub Desktop.
Save sb8244/74dcf03e472560ca80bffaf94a73ade1 to your computer and use it in GitHub Desktop.
defmodule SteveTrollsElixir.Private do
defmacro __using__(_) do
quote do
import unquote(__MODULE__)
@on_definition {unquote(__MODULE__), :on_priv_def}
end
end
defmacro private() do
quote do
Module.put_attribute(__MODULE__, :private, true)
end
end
def on_priv_def(%Macro.Env{module: mod}, kind, name, args, _guards, _body) do
private_context? = Module.get_attribute(mod, :private)
if private_context? && kind != :defp do
raise "Defined a public function in a private section. Define #{name}/#{length(args)} above the private definition or mark it defp"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment