Erlang:有没有办法从我的模块中导出其他模块的导出?
我正在为 webmachine 编写一些资源,它们共享许多相同的功能。所以我想知道是否可以在单独的模块中编写常用函数,然后以某种方式包含它,并让资源自动导出其导出(而不必从每个资源显式导出它们)。
I'm writing several resources for webmachine that share lots of same functions. So I'm wondering if it's possible to write the common functions in a separate module, then somehow include it, and have its exports automatically exported by the resource (without having to export them from every resource explicitly).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我们有一个包装器模块,它实现所有 Webgear 回调,并将它们传输到真正的实现模块(如果在那里实现)。该模块可以对某些部分有特殊的实现,甚至可以在使用它的模块中启用新的回调。基本上,该模块是所有其他资源的包装资源。
首先,您的调度映射如下所示:
为此,您需要发现实际实现模块实现的回调:
这将初始化一个基本 Webgear 资源,该资源包含有关其状态中的真实回调模块的信息。
然后,对于包装器资源的每个回调(如果您希望实现模块能够使用该资源,则必须实现该资源),您将查看该函数是否已实现并在那里处理它,使用此函数:
例如,
call/4
函数在包装器模块中的使用方式如下:这在我们的项目中效果很好,其中所有资源共享一些通用逻辑(在这样的包装器模块中实现)。虽然还没有对性能进行太多的基准测试,但开销应该相当小(一次字典查找和一次额外的模块调用,再加上一些记录争论)。
We have a wrapper module that implements all Webgear callbacks and transfers them to the real implementation module if they are implemented there. That module can have special implementations for certain parts or even enable new callbacks in the modules using it. Basically that module is a wrapper resource for all your other resources.
First, your dispatch map would look as follows:
To do this, you need to discover what callbacks your actual implementation module implements:
This will initialize a basic Webgear resource that has information about the real callback module in its state.
Then, for every callback to the wrapper resource (which you must implement if you want your implementation modules to be able to use then), you would see if that function was implemented and handle it there, using this function:
The
call/4
function is, for example, used like this in the wrapper module:This works well in our project where all resources share some common logic (implemented in such a wrapper module). Haven't benchmarked the performance very much though, but the overhead should be fairly small (one dict lookup and one extra module call, plus some record wrangling).
实际上这是可能的,但只能通过未记录的功能。模块继承。
Actually it is possible, but only through an undocumented feature. Module inheritance.
是的,您可以使用混合器库,https://github.com/opscode/mixer 它是一个编译器解析转换将完全按照您的要求进行。我到处都用它来进行网络机器回调
Yes you can use the mixer library, https://github.com/opscode/mixer it is a compiler parse transform that will do exactly what you want. I use it all over the place for webmachine callbacks
不,这是不可能的。我所做的是将我的应用程序所需的所有回调实现到通用函数。另一种方法是修补 webmachine 以从函数中获取 cb,而不是通过查看导出。
No,it is not possible. What I did was to implement all the callbacks needed by my app to the generic function. Another way would be to patch webmachine to fetch the cb's from a function instead of by looking at exports.