-
-
Save collegeimprovements/f9d41b7ce42b02feda45cf6853376951 to your computer and use it in GitHub Desktop.
Spec to Callback
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.Foo do | |
@on_definition MyApp.SpecToCallback | |
@spec bar(String.t()) :: String.t() | |
def bar(foobar) do | |
impl().bar(foobar) | |
end | |
defp impl, do: Application.get_env(:my_app, :my_app_foo_impl, __MODULE__.DefaultImpl) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.Foo.DefaultImpl do | |
@behaviour MyApp.Foo | |
@impl true | |
@spec bar(String.t()) :: String.t() | |
def bar(foobar) do | |
foobar | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.SpecToCallback do | |
def __on_definition__(env, :def, name, args, _guards, _body) do | |
Module.spec_to_callback(env.module, {name, length(args)}) | |
end | |
def __on_definition__(_, _, _, _, _, _), do: nil | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://elixirforum.com/t/knigge-an-opinionated-approach-to-behaviours/30908/2