[erlang-patches] Some cleanups in kernel files

Kostis Sagonas <>
Wed Jan 30 15:16:49 CET 2013


On 12/20/2012 03:02 PM, Fredrik wrote:
>   Hello Kostis,
> There has been raised some questions regarding this patch from the OTP team.
> In this commit:
> https://github.com/kostis/otp/commit/3a13b7a1e1bdd48b0293fc7366c3d133abf845bd
> You are returning 'ok' from a case clause which does not an affect on
> the returned value,
>
> {noreply,  S};
>
> We would like an explanation why this is so important.

Replying to this mail has been long on my TODO list but was apparently 
low priority.

Strictly this change is not necessary. However, it shuts off a dialyzer 
warning when the option -Wunmatched_returns is used (perhaps the name of 
the option is inaccurate in this case) and makes the code slightly 
cleaner in the following sense:

In a functional language, all expressions have return values which, at 
least in principle, are produced in order to be consumed somewhere in 
the continuation. I.e. in a case statement in some function:

   foo() ->
      .... SOME CODE ...
      case ... of
         PATTERN_1 -> .... LAST_CALL_1;
         ...
         PATTERN_N -> .... LAST_CALL_N
      end,
      ... MORE CODE ...

I think it's a good programming practice that the N branches return some 
value (such as 'ok') that makes it clear that the return (of the case 
expression) is to be ignored.

This allows for e.g. a transformation that moves the case into some 
other function if needed, as e.g. in:

   foo() ->
      .... SOME CODE ...
      bar(...),
      ... MORE CODE ...

   bar(...) ->
      case ... of
         PATTERN_1 -> .... LAST_CALL_1;
         ...
         PATTERN_N -> .... LAST_CALL_N
      end.

and does not result in unmatched return warnings.


An alternative to the above is of course to "consume" the return value 
of the case statement as in:

   foo() ->
      .... SOME CODE ...
      _ = case ... of
             PATTERN_1 -> .... LAST_CALL_1;
             ...
             PATTERN_N -> .... LAST_CALL_N
          end,
      ... MORE CODE ...


Hope this explains the issue.

By the way, I do not have a strong opinion on whether this patch should 
be included as is, but I do have a request that at least the "main" OTP 
applications, such as kernel in this case, soon become free of unmatched 
return dialyzer warnings. This patch is just a step in this direction.

Kostis

> On 12/18/2012 03:04 PM, Kostis Sagonas wrote:
>> The code in some kernel modules has been cleaned up so that functions
>> do not return terms of wrong type by accident (most notably by having
>> a call to !/2 in a return position) and so that language constructs
>> that reflect in a more accurate way the program's intention are used.
>>
>> Please include:
>>
>> git fetch git://github.com/kostis/otp.git kernel-cleanup
>>
>> Kostis
>> _______________________________________________
>> erlang-patches mailing list
>> 
>> http://erlang.org/mailman/listinfo/erlang-patches
>



More information about the erlang-patches mailing list