Skip to content

Instantly share code, notes, and snippets.

@googya
Created December 11, 2025 15:44
Show Gist options
  • Select an option

  • Save googya/6229c2d7d34d4dc4891eb1e69860536c to your computer and use it in GitHub Desktop.

Select an option

Save googya/6229c2d7d34d4dc4891eb1e69860536c to your computer and use it in GitHub Desktop.
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