[erlang-questions] How to code against an interface rather than an implementation?

Chris King colanderman@REDACTED
Fri Aug 9 15:58:54 CEST 2013


On Fri, Aug 9, 2013 at 8:58 AM, Loïc Hoguin <essen@REDACTED> wrote:
> Whether Dialyzer can do that or not is a bit irrelevant, as this kind of
> error is quickly apparent when your tests immediately fail.

Eh, that argument can apply to most things a static analyzer checks.
The point of a static analyzer is to catch these kinds of errors
without having to write formal tests.

Hans: You can get most of what you want by using the (undocumented)
callback syntax to specify the interface (in addition to the pattern
you described above).  Its syntax is identical to that of "spec"
function specifications; use it in the behavior module to declare the
types of the behavior's functions.  See
http://learnyousomeerlang.com/dialyzer#typed-behaviours for an
example.

Specifying callback types will check that modules claiming to
implement that behavior will be type-checked by Dialyzer.  However
Dialyzer won't be able to check modules that *don't* specify which
behavior they implement, and it won't be able to check that the
front-end module is using the back-ends according to your behavior
spec (there is (AFAIK) currently no way to specify this).

(Dialyzer *can* check, I think, that the front-end module is using the
*currently configured* back-end in a safe manner (not necessarily
according to the behavior spec though).  Though I wouldn't rely on
this, as it may be a long enough code path between back-end
declaration and use to result in a false negative.)



More information about the erlang-questions mailing list