Implementing interfaces in Erlang

前端 未结 2 1396
后悔当初
后悔当初 2021-02-14 23:24

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 interface

相关标签:
2条回答
  • 2021-02-14 23:50

    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.

    0 讨论(0)
  • 2021-02-15 00:13

    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.

    0 讨论(0)
提交回复
热议问题