Elixir重命名并包装Erlang模块? [英] Elixir rename and wrap Erlang module?
问题描述
是否可以重命名现有的Erlang模块?我在几个Erlang模块中有一些要在Elixir项目(断言库)中使用的代码.
Is it possible to rename an existing Erlang module? I have some code in several Erlang modules that I want to use in an Elixir project (assertion library).
我不想在完成所有Erlang模块后将其转换为Elixir,但我想重命名它们,并可能向其中添加其他功能.这两个模块只是功能的集合,它们不执行行为或执行任何异常操作.
I don't want to convert all the Erlang modules to Elixir as they are complete, but I want to rename them, and possible add additional functions to them. Both modules are simply collections of functions, they don't implement behaviors or do anything unusual.
我希望能够使用现有的Erlang模块:
I want to be able to take an existing Erlang module:
-module(foo_erl).
-export([some_fun/1]).
some_fun(Arg) ->
ok.
并编写一个Elixir模块来扩展Erlang:
And write an Elixir module to extend the Erlang one:
defmodule ExFoo do
somehow_extends_erlang_module :foo_erl
another_fun(arg) do
:ok
end
end
然后可以使用Erlang模块中定义的功能:
And then be able to use the functions defined in the Erlang module:
iex(1)> ExFoo.some_fun(:arg) #=> :ok
在Elixir中这可能吗?如果是这样,我将如何去做?
Is this possible in Elixir? If so, how would I go about doing this?
推荐答案
通常,我们建议避免包装Erlang模块.如果您想以自动方式包装许多模块,velimir的帖子就在现场.
In general, we suggest to avoid wrapping Erlang modules. In case you want to wrap many modules in an automatic fashion, velimir's post is right on the spot.
但是,如果您有一次性的情况,您肯定要包装Erlang模块,那么我建议您简单地使用defdelegate
:
If you have one-off case though, where you definitely want to wrap an Erlang module, I would suggest to simply use defdelegate
:
defmodule MyLists do
defdelegate [flatten(list), map(fun, list)], to: :lists
end
这篇关于Elixir重命名并包装Erlang模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!