elixir before_compile && on_definition 简单使用
elixir 提供了一些方便的模块属性,可以让我们方便的进行elixir 的扩展(尤其是进行元编程的时候)
比如elixir_agent 就使用到了before_compile以及on_definition ,方便进行trace 的处理
on_definition 的简单使用
- 参考demo
内容来自官方,可以实现输出函数的一些信息
defmodule Hooks do
def on_def(_env, kind, name, args, guards, body) do
IO.puts("Defining #{kind} named #{name} with args:")
IO.inspect(args)
IO.puts("and guards")
IO.inspect(guards)
IO.puts("and body")
IO.puts(Macro.to_string(body))
end
end
defmodule MyModule do
@on_definition {Hooks, :on_def}
def hello(arg) when is_binary(arg) or is_list(arg) do
"Hello" <> to_string(arg)
end
def hello(_) do
:ok
end
end
before_compile 的简单使用
- 参考demo
defmodule MyModule do
@before_compile {Dalong, :__before_compile__}
def hello(arg) when is_binary(arg) or is_list(arg) do
"Hello" <> to_string(arg)
end
def hello(_) do
:ok
end
end
Dalong 模块中__before_compile__ 的定义
defmacro __before_compile__(env) do
IO.puts "Before compilation!"
IO.inspect env
quote do
IO.puts "Before compilation!"
end
end
elixir_agent 的使用
elixir_agent 的使用是自己开发了一个macro,通过use 统一导入,使用如下
defmacro __using__(_args) do
quote do
require NewRelic
require NewRelic.Tracer.Macro
require NewRelic.Tracer.Report
Module.register_attribute(__MODULE__, :nr_tracers, accumulate: true)
Module.register_attribute(__MODULE__, :nr_last_tracer, accumulate: false)
@before_compile NewRelic.Tracer.Macro
@on_definition NewRelic.Tracer.Macro
end
end
说明
elixir 的一些玩法比较有意思,看似缺少一些语言的能力,但是kernel 提供的其他功能,实现起来反而更加灵活强大,以下参考链接中对于上述属性
有完整的介绍可以看看
参考资料
https://hexdocs.pm/elixir/1.14.5/Module.html
https://github.com/newrelic/elixir_agent
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2023-01-15 step-ca 测试试用
2023-01-15 smallstep certificates 开源ca 管理工具
2022-01-15 data transfer project google 开源的开放服务数据传送框架
2021-01-15 k6 新的扩展参考开发
2021-01-15 k6 新的扩展开发模式
2021-01-15 gocloc 代码统计算法
2020-01-15 grokdebug 一个方便的grok 调试工具