首页 > 其他分享 >elixir macro defoverridable 功能

elixir macro defoverridable 功能

时间:2024-01-08 18:01:25浏览次数:39  
标签:__ do end macro init elixir login defoverridable

基于macro 的包装函数可以使用defoverridable 可以方便进行方法的重写

参考使用

  • app.ex
defmodule App do
  defmacro  __using__(_options) do
    quote do
      def init do
        IO.puts "do init"
      end
 
      def login do
        IO.puts "do login"
      end
 
      def app_init do
        init()
        login()
      end
 
      defoverridable init: 0,login: 0
    end
  end
end
demo.ex
defmodule Demo do
  use App
 
  def init() do
    IO.puts "override do init"
  end
 
end
defmacro  __using__(_options) do
  • 效果

如果demo 中没有重写init 会输出

elixir macro defoverridable 功能_参考资料

但是因为我们重写了init 方法,效果如下

elixir macro defoverridable 功能_html_02

说明

此方法还可以应用到Behaviour 中,phoenix 的pipeline 就使用了此方法

参考资料

https://blog.appsignal.com/2021/10/26/how-to-use-macros-in-elixir.html
https://hexdocs.pm/elixir/1.12/Kernel.html#defoverridable/1

标签:__,do,end,macro,init,elixir,login,defoverridable
From: https://blog.51cto.com/rongfengliang/9148285

相关文章

  • wasmex webassenbly elixir 运行时
    wasmex是基于wasmtime以及rustnif开发的方便elixir运行webassembly的框架与rust的集成与rust集成使用的三方包 与mjml工具类似使用了rustler_precompiled以及rustlerrust使用的三方包 前边也说了是基于了wasmtime包装的,同时使用了wasmtimewasi一些子模块说明rustle......
  • elixir erlang 简单调用学习
    实际上基于elixir的mix进行erlang以及elixir的互调用开发处理是很方便的,mix直接就包含了构建erlang代码同时对于代码的互调用,只要使用符合语言格式要求就行了,以下是一个简单的互调用学习项目准备项目结构 ├──README.md├──lib│├──a.e......
  • rebar3 集成elixir 模块
    社区包含了一个rebar3的elixirmix插件,可以方便rebar使用elixir模块参考使用rebar.config配置{erl_opts,[debug_info]}.{deps,[%添加引用{decimal,"2.0.0"}]}.{shell,[{apps,[basic]}]}.%添加插件{plugins,[rebar_mix]}.{provider_hooks,[{......
  • elixir mjml_nif 试用
    mjml_nif是使用rustler包装的mrml基于了erlang的nif接口实现的elixir模块,同时为了方便预编译nif的使用,使用了rustler_precompiled,以下是一个简单的试用项目试用添加依赖defpdepsdo[{:mjml,"~>3.0"}]end代码集成lib/mail/rong.mail.exdefmoduleRo......
  • elixir mjml_nif 试用
    mjml_nif是使用rustler包装的mrml基于了erlang的nif接口实现的elixir模块,同时为了方便预编译nif的使用,使用了rustler_precompiled,以下是一个简单的试用项目试用添加依赖 defpdepsdo[{:mjml,"~>3.0"}]end代码集......
  • rebar3 引用本地elixir 模块
    前边简单说过基于rebar_mix使用elixir模块,但是使用的模块是三方的,如果时候我们可以需要使用自己的就可以使用本地git项目,或者搭建自己的私服git,以下是一个简单使用项目准备本地elixirmix项目一个基于mixcli创建的项目,同时进行gitinit mixnewlogingit......
  • rebar3 集成elixir 模块
    社区包含了一个rebar3的elixirmix插件,可以方便rebar使用elixir模块参考使用rebar.config配置{erl_opts,[debug_info]}.{deps,[%添加引用{decimal,"2.0.0"}]}. {shell,[{apps,[basic]}]}. %添加插件 {plugins,[......
  • Slint 的两个核心 macro
    slint::include_modules!要使用这个库,首先需要在Cargo.toml中完成:[package]部分增加build="build.rs"[build-dependencies]部分增加slint-build="1.1.1"[1]随后,应该在与Cargo.toml同级的目录创建build.rs,其内容为(示例):fnmain(){slint_build::compile("ui/appw......
  • Laravel Macro 让你的代码更简洁,更具有可读性
    来源:http://www.shanhubei.com/archives/2806.html你可以把它理解成为 trait 中的一个方法,还有点和我们开发中常用助手文件中 helpers 中的方法类似,其目的是将Laravel的内部组件进行横向扩展以全局通用。下面我们来看一个例子:User 表中有一个字段是 is_vip,用来记录用户......
  • Go每日一库之152:gomacro(终端运行go代码)
    [gomacro](https://github.com/cosmos72/gomacro)是一个近乎完整的Go解释器,用纯Go实现,它同时提供交互式REPL和脚本模式,并且在运行时不需要Go工具链(除了一些非常特殊的场景:在运行时导入第三方包)。它在Go标准库之外有两个依赖项:github.com/peterh/liner和golang.org/x/......