在 Erlang 中实现接口
如何在erlang中实现一个接口?模块的结构是什么或如何设置?
我有一个分层架构,想要实现每一层中的接口。允许需要下层处理某些任务的层访问或调用这些任务。
该接口模块的功能如何?
How does one implement an interface in erlang? What is the structure of the modules or how is it setup?
I have a layered architecture and want to implement the interfaces in each layer. Allowing the layer which requires the below layer to handle some task to access or call for these tasks.
How would this interface module look in terms of functionality?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Java 接口或 ML 模块签名意义上的接口在动态类型的 Erlang 世界中并不存在。您必须在评论中记录该行为或提供 -spec 合同供透析器查看。
一般来说,接触一门语言的最佳方法是不要假设你可以将现有知识过多地映射到它中,除非你碰巧知道一种概念上“接近”的语言。与 Erlang 接近的语言是 Prolog 和Scheme。更远的是 Python 和 Ruby,但它们对 OOP 设计的依赖使它们进入了视野。任何静态类型,Ocaml、Haskell、Java、C#、C++ 等等绝对是遥不可及的。
An interface in the sense of a Java interface or an ML module signature does not exist in the dynamically typed Erlang world. You will have to document the behavior in a comment or provide a -spec contract for the dialyzer to look at.
In general, the best way to approach a language is by not presuming you can map your existing knowledge too much into it, unless you happen to know a language that is "close" in concepts. Languages close to Erlang are Prolog and Scheme. Farther out comes Python and Ruby, but their reliance on an OOP design puts them into the horizon. Anything statically typed, Ocaml, Haskell, Java, C#, C++ and so on are definitely beyond the horizon.
Erlang 中最接近的概念是用户定义的行为。然而,它们往往很少被使用。请注意,唯一检查的是函数的存在性和数量。您无法测试该模块是否实际实现了某些行为;您只需调用回调函数,如果模块碰巧“意外”导出具有相同名称的函数,那么您就不走运了。
The closest concept in Erlang is user-defined behaviour. However, they tend to be used quite rarely. Note that the only thing which is checked is existence and arity of functions. You can't test that the module actually implements some behaviour; you just call the callback functions and if the module happens to "accidentally" export functions with same names, you are out of luck.