What Erlang/BEAM related stuff are you doing?

I have been working on implementing the compiler part of EEP 49: Value-Based Error Handling Mechanisms by @MononcQc. The EEP was first submitted in 2018, and later revised by Fred in late 2020. The latest revision addresses notes from the OTP Technical Board.

The idea of the EEP is to eliminate deeply nested caseend constructs such as the following code from Mnesia:

commit_write(OpaqueData) ->
    B = OpaqueData,
    case disk_log:sync(B#backup.file_desc) of
        ok ->
            case disk_log:close(B#backup.file_desc) of
                ok ->
                    case file:rename(B#backup.tmp_file, B#backup.file) of
                       ok ->
                            {ok, B#backup.file};
                       {error, Reason} ->
                            {error, Reason}
                    end;
                {error, Reason} ->
                    {error, Reason}
            end;
        {error, Reason} ->
            {error, Reason}
    end.

Using the new maybeend construct described in the EEP, the code can be simplified to:

commit_write(OpaqueData) ->
    maybe
        ok <~ disk_log:sync(OpaqueData#backup.file_desc),
        ok <~ disk_log:close(OpaqueData#backup.file_desc),
        ok <~ file:rename(OpaqueData#backup.tmp_file, OpaqueData#backup.file),
        {ok, OpaqueData#backup.file}
    end.

@MononcQc and @peerst implemented a first proof of concept during the Spawnfest in September.

That implementation and a previous revision of the EEP reused the begin keyword instead of introducing the new maybe keyword to avoid backward incompatibilities with code that used maybe as a function name or atom, for example the compiler itself.

The OTP Technical Board decided that the maybe keyword should be used. By default, maybe would remain an atom, and the maybeend would not be available. To use the maybe construct, a compiler option to enable it would be needed. The option can be applied to individual modules, so one part of a code base can use maybe as an atom, while some modules in the code base can use the maybeend construct. There is currently ongoing work on an EEP and an implementation of a mechanism that will allow adding new features that can be enabled with an option but by default are disabled.

I have implemented the compiler part of EEP 49 as a draft EEP.

17 Likes