EEP: XXX Title: More Fun instead of MFA Version: $Revision: 14 $ Last-Modified: $Date: 2007-06-29 16:24:01 +0200 (Fri, 29 Jun 2007) $ Author: Richard A. O'Keefe Status: Draft Type: Standards Track Erlang-Version: R13B-3 Content-Type: text/plain Created: 04-Dec-2009 Post-History: Abstract Add a hibernate/1 function analogous to hibernate/3. More generally, hunt down all uses of M, F, A and offer Fun alternatives. Specification Erlang R13B-3 offers erlang:hibernate(Module, Function_Name, Argument_List) which shrinks the process stack back and restarts the process with apply(Module, Function_Name, Argument_List) as soon as there is a message in its mailbox. Add a new function to the erlang: module that acts as if it were defined something like hibernate(Fun) when is_function(Fun, 0) -> hibernate(erlang, apply, [Fun,[]]). Do the same for proc_lib. Motivation The elimination of 'internal exports'. There's a lot of Erlang code with 'Internal exports'. The functions thus exported appear to fall into three classes: (A) behaviour callbacks which are not internal callbacks at all, and should be documented as behaviour callbacks; (B) functions to be invoked by spawn/3, spawn/4, spawn_link/3, spawn_link/4, or some other variation of spawning, which can now be better handled by passing funs to be invoked by spawn/1, spawn/2, spawn_link/1, spawn_link/2, or some other kind of spawn that takes a fun argument; (C) functions to be invoked by hibernate/3 or some other Erlang library function with an MFA interface and no related Fun interface. In new code, group (A) must still be exported, but should be documented as behaviour callbacks, and associated with the behaviour they are callbacks of. Group (B) should simply never exist. Such instances as remain in old code can be gradually replaced during maintenance. But the group (C) functions have no alternative. Internal functions that are exported are a hazard. Consistency. If some Erlang/OTP library functions with an MFA interface have Fun analogues, it is better if all of them do. Note that I have specified only one such function because it is the only one I'm currently aware of, but the EEP is about fixing *all* such anomalies. Rationale The implementation of hibernate/1 given above is exactly the way that the Fun interfaces of the spawning functions are currently implemented in kernel/src/erlang.erl. Backwards Compatibility There is no new syntax, no removed library functions, and no change to the semantics of any existing library function, so there should be no backwards compatibility issues. Reference Implementation Add the following code to kernel/src/erlang.erl -export([hibernate/1]). % Hibernate with a fun hibernate(Fun) when is_function(Fun, 0) -> hibernate(erlang, apply, [Fun, []]); hibernate({M,F}) when is_atom(M), is_atom(F) -> hibernate(erlang, apply, [M, F, []]); hibernate(F) -> erlang:error(badarg, [F]). In the documentation for erlang:hibernate/1, change "is awaken" to "is awoken". Add the following documentation: erlang:hibernate(Fun) Types: Fun = fun() This has the same effect has hibernate/3 except that when the process is awoken it restarts by calling Fun() instead of apply(Module, Function, Arguments). See also proc_lib:hibernate/1. Add the following code to stdlib/src/proc_lib.erl -export([hibernate/1]). % Hibernate with a fun hibernate(Fun) when is_function(Fun, 0) -> hibernate(erlang, apply, [Fun, []]); hibernate({M,F}) when is_atom(M), is_atom(F) -> hibernate(erlang, apply, [M, F, []]); hibernate(F) -> erlang:error(badarg, [F]). Add the following documentation for proc_lib: hibernate(Fun) Types: Fun = fun() This function does the same as (and calls) the BIF erlang:hibernate/1, but ensures that exception handling and logging continue to work as expected when the process wakes up. Always use this function instead of erlang:hibernate/1 for processes started using proc_lib functions. References None. Copyright This document has been placed in the public domain. Local Variables: mode: indented-text indent-tabs-mode: nil sentence-end-double-space: t fill-column: 70 coding: utf-8 End: