Created
December 11, 2025 15:44
-
-
Save googya/6229c2d7d34d4dc4891eb1e69860536c to your computer and use it in GitHub Desktop.
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 Demo do | |
| IO.puts "1. Demo 模块定义时" | |
| defmacro test do | |
| IO.puts "2. 宏被调用时(展开时)" | |
| x = __MODULE__ # 这时求值 | |
| quote do | |
| IO.puts "3. 注入到调用者后执行" | |
| IO.puts unquote(x) | |
| end | |
| end | |
| end | |
| defmodule User do | |
| IO.puts "4. User 模块开始定义" | |
| require Demo | |
| IO.puts "5. require 完成" | |
| Demo.test() | |
| IO.puts "6. 宏调用完成" | |
| end | |
| # 1. Demo 模块定义时 | |
| # 2. 宏被调用时(展开时) ← 在 "4" 之前! | |
| # 4. User 模块开始定义 | |
| # 5. require 完成 | |
| # 3. 注入到调用者后执行 | |
| # Elixir.Demo | |
| # 6. 宏调用完成 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment