[erlang-questions] Dialyzer cannot catch gen_server callback return type error

Maxim Treskin zerthurd@REDACTED
Sun Jul 8 14:24:03 CEST 2012


On 8 July 2012 07:13, Sven Heyll <sven.heyll@REDACTED> wrote:

> Acutally in gen_server I find:
>
> -callback handle_cast(Request :: term(), State :: term()) ->
>     {noreply, NewState :: term()} |
>     {noreply, NewState :: term(), timeout() | hibernate} |
>     {stop, Reason :: term(), NewState :: term()}.
>
> Why not define it this way:
>
> -callback handle_cast(Request :: term(), State) ->
>     {noreply, State} |
>     {noreply, State, timeout() | hibernate} |
>     {stop, Reason :: term(), State}.
>
> Is this possible?? I think this would help well written apps that are
> careful with type consistency, and hurt only bad code - but one who
> writes bad code wouldn't invoke dialyzer.
>
> No, it is impossible. What type of State would be here?


> wbr
> Sven
>
>
> 2012/7/6 Maxim Treskin <zerthurd@REDACTED>:
> >
> >
> > On 6 July 2012 17:35, Stavros Aronis <aronisstav@REDACTED> wrote:
> >>
> >> Hi again,
> >>
> >> The signature (or "success typing" in Dialyzer's terminology) is
> >> `my_module:handle_cast(term(),term()) -> {'noreply',term()}` because the
> >> second clause:
> >>
> >> handle_cast(_Msg, State) ->
> >>     {noreply, State}.
> >>
> >> will accept any terms as _Msg and State and return {noreply, State} in
> >> which State again is any term. In Dialyzer's type system, this {noreply,
> >> term()} return value will 'absorb' the definitely incorrect {noreply,
> >> {bugotak}} as {bugotak} is also a term(). From that point on, Dialyzer
> can
> >> assume that term() might also be #state{} and emit no warnings. This
> happens
> >> because Dialyzer checks the whole range of return values at once and not
> >> each particular clause. If all the return values were definitely
> different
> >> than #state{}, then and only then would Dialyzer warn you.
> >>
> >
> > Thanks for the clarification, Stavros! Now I understand how Dialyzer
> checks
> > types of function clauses.
> >
> >>
> >> Again, trying to check each particular return value against a given spec
> >> might be an interesting extension and we will consider it in the future.
> >>
> >
> > I hope so. It would be a significant level up!
> >
> >>
> >> Stavros
> >>
> >> On Fri, Jul 6, 2012 at 11:39 AM, Maxim Treskin <zerthurd@REDACTED>
> wrote:
> >>>
> >>> I just really not understand. Function handle_cast returns, and returns
> >>> incorrect type according to -spec signature, right?
> >>> Why Dialyzer lose this information? It is sort of missbehaviour i
> think.
> >>>
> >>> --
> >>> Max Treskin
> >>
> >>
> >
> >
> >
> > --
> > Max Treskin
> >
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://erlang.org/mailman/listinfo/erlang-questions
> >
>



-- 
Max Treskin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120708/60f40eb5/attachment.htm>


More information about the erlang-questions mailing list