Specs for Callback Functions

I tried to add a spec to the handle_cast callback function from the GenServer behavior, but Dialyzer outputs conflicting advice on what the success typing is.

Perhaps what I am doing is pointless as the callback is defined by a spec in the first place. Or perhaps there is a way - if nothing else it gives a reader of the code a better idea of what’s going on.

Any advice?

1 Like

The spec is already defined in the GenServer behaviour. You can safely remove it.

I guess you would like to make a public API; The module should hide the implementation details, so the user should not call GenServer.call, etc.

1 Like

Nope, it was really more of a documentation / elucidation effort, nothing to do with an API. Thanks for the advice though.

OK, for completeness I notice that the init callback can have a spec attached… e.g. this works

@spec init(any) :: {:ok, %{}}

so perhaps it’s possible for other callbacks also, damned if I know how though!

Well, in that case #-comments would do.

In general @doc false are added to callbacks. As I said, these are implementation details that you don’t want in your public API documentation.

1 Like