-
Notifications
You must be signed in to change notification settings - Fork 2
convert inject/1 to a macro #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,19 +8,25 @@ defmodule Inject do | |
| :ok | ||
| end | ||
|
|
||
| defmacro inject_module(mod) do | ||
| if Application.get_env(:inject, :disabled) do | ||
| mod | ||
| else | ||
| quote bind_quoted: [mod: mod] do | ||
| Inject.Registry | ||
| |> Registry.lookup(mod) | ||
| |> Enum.reverse() | ||
| |> find_override() || mod | ||
| end | ||
| end | ||
| end | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The best way to visualise macros (well, for me at least) is to think of them as copy & paste. def fancy do
i(Dep).fun()
endreplaced with def fancy do
(
if Application.get_env(:inject, :disabled) do
mod
else
Inject.Registry
|> Registry.lookup(mod)
|> Enum.reverse()
|> find_override() || mod
end
).fun()
endThis would still call the What we want instead, is to evaluate the defmacro inject(mod) do
# run stuff here, during compilation
if letsdothis do
quote(do: injectstuff)
else
quote(do: nah)
end
This way the body of Now, what should Can't wait to see the v2! 🎸
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I took a stab at V2. I just amended the commit; should have just squashed down before merging so you could see the change history but oh well, at least the change is small! I also asked a separate question about mod which you can see in the diff. By the way, the test in |
||
|
|
||
| def i(mod) do | ||
| inject(mod) | ||
| end | ||
|
|
||
| def inject(mod) do | ||
| if Application.get_env(:inject, :disabled) do | ||
| mod | ||
| else | ||
| Inject.Registry | ||
| |> Registry.lookup(mod) | ||
| |> Enum.reverse() | ||
| |> find_override() || mod | ||
| end | ||
| inject_module(mod) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Almost there :) Calling
|
||
| end | ||
|
|
||
| defp find_override([]), do: nil | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that I didn't wrap this in a quote block. I can do it if needed, but I think it would be redundant since we're not modifying the incoming atom or AST (not that I would see inject/1 receiving an AST any time soon).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, doing
quote(do: mod)here throws warnings and I'm not sure why.... 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since quote is kinda like literal copy paste, what would be the result of such code?