Interface type safety

How to check behaviour safeness in spec code?

Imagine situation, when you pass some Module in the function. Some methods from this module should be called in your function, and to ensure there existence you can define behaviour.
So the question is - how to spec the fact that Module that is passed to your function implements specific behaviour.

If it’s not done like this - then what’s the sense of behaviours at all???

There is no way to check that. The value of behaviours is that if you say:

defmodule Foo do
  @behaviour Bar

The compiler will warn if Foo does not implement one of the callbacks that Bar requires.

1 Like

But if I should check it by myself, then what’s the sense of behaviours at all?

But if I should check it by myself

Check what? You should not check that a module given to a function implements the behaviour. A better route is to assume it does and have it blow up if it does not.

Either way that’s still a fundamentally different kind of check than one that happens at compile time with the warnings I mentioned before.

Elixir is a dynamic language, using behaviours does not change that.