Allow module attributes before, between, or after complete function declarations, while retaining existing rules for the module declaration and order-dependent constructs. In particular, exports and imports remain module-wide regardless of their position. This removes a source layout restriction without introducing new syntax, attribute scope, or runtime behaviour.
Related declarations are easier to maintain when kept together. An export may be placed beside the function it exposes, rather than in a distant header list. User-defined metadata can similarly accompany related code.
Erlang already permits declarations such as function specifications, types, records, and documentation among function declarations. Requiring other module attributes to precede every function is an inconsistent restriction on source organisation.
A module attribute may occur at any top-level form boundary, including between or after complete function declarations, subject to the unchanged restrictions below. No new grammar or keywords are added.
Module-wide attributes retain module-wide meaning. The position of an export does not affect which functions it exports. An import applies to calls throughout the module, including calls preceding the import and guard calls where the imported function is legal.
Attribute-specific validation and rules for repeated attributes remain unchanged. For example, export lists are combined as before, and conflicting imports and local definitions remain errors. Proximity to a function does not introduce function-local attribute scope.
The module declaration retains its existing placement, uniqueness, and name-validation rules. Record definitions must still precede their uses. Preprocessor directives retain their existing ordering and effects, including macro, include, and feature-directive rules.
Documentation attributes retain their existing association rules. This proposal does not make documentation placed after a function document that preceding function.
Attributes remain ordinary forms in their original source order. They are not moved into a synthetic header. The abstract-format representation is unchanged.
For example, this layout becomes valid:
-module(example).
-export([sum/1]).
sum(Numbers) -> lists:sum(Numbers).
-export([reversed/1]).
reversed(Items) -> reverse(Items).
-import(lists, [reverse/1]).
-custom_attribute(reversed/1).
The final import resolves the earlier call to reverse/1 as
lists:reverse/1. The custom attribute remains module metadata.
Treating imports as module-wide avoids layout-dependent name resolution and matches their existing meaning. Extending the exception list only for exports would leave the same inconsistency for other attributes.
Relaxing record-before-use or preprocessor ordering would be a different change and is deliberately excluded. Function clauses must still form a contiguous declaration.
Previously valid modules retain their existing meaning. No BEAM format or runtime changes are required. Modules using the newly permitted layouts will still be rejected by older compilers; code targeting those compilers must retain the existing layout.
Tools and parse transforms must process the whole form list rather than assuming that all module attributes precede the first function. Existing attribute nodes and their source order are preserved. This EEP permits new layouts; it does not require rewriting existing modules.
The reference implementation is OTP pull request 12232.
It confines production changes to erl_lint, checks the module header
once, uses one form handler for attributes and functions, and collects
function imports before checking bodies. Tests and the language
reference are updated alongside it.
OTP-30.0 is the proposed release target, not an accepted release promise.
This document is placed in the public domain or under the CC0-1.0-Universal license, whichever is more permissive.