[erlang-questions] Erlang 19 callbacks aren't attributes?

Alex S. <>
Wed Jan 25 11:22:31 CET 2017


> 25 янв. 2017 г., в 13:00, Richard Carlsson <> написал(а):
> 
> There are some distinctions that have not been well documented. I'll try to clarify them based on how things actually work.
> 
> First, an Erlang program is divided into "forms" - sequences of tokens that are terminated by a full stop. Forms are either function definitions (starting with a function name, i.e., an atom) or what's generally called "attributes", which start with a minus and an atom, as in '-module(..).'. Seems straightforward, right?
> 
> There are however three kinds of forms in Erlang that have the general shape of attributes:
> 
> * Preprocessor Directives - things like '-include("foo.hrl").' or '-ifdef(DEBUG)'. These only make sense to the preprocessor (epp), and are removed by the preprocessing step.
> 
> * Declarations - things like module name, export/import lists, record definitions, type specifications, type definitions, and behaviour callbacks. Also some more unknown special declarations like '-file(...).' that the preprocessor inserts to tell the compiler which parts of the code came from which files, or errors and warnings passed on from preprocessing. The common thing about Declarations is that they have meaning on the Erlang Language level, and typically have particular rules for how they may look (a module declaration must provide a single module name, an import declaration must provide a module name and a list of function/arity pairs, a record declaration must list fields and possibly types, and so on), and where in the file they may occur (before any function, or interspersed with functions). Since these are part of the language, they are not included in the 'attributes' list of the Beam file; the corresponding information is generally found in some other part of the file.
> 
> * "Wild" Attributes (as they were named in the draft Standard Erlang specification) - any attribute that is not one of the Declarations, and has the form '-Atom(Term)' - note that only one argument is allowed. These can occur anywhere in the file and have no special meaning except possibly to various tools. For example, '-compile(Options).' only has a meaning to the specific Erlang compiler that you're using; it's not part of the language. These are just annotations, and these are the ones that are listed in the 'attributes' section of the module info.
> 
> These distinctions have not been properly documented, but have been intuitively followed by the people working on the language and compiler. In some cases, the lines are blurred. For example, one could easily argue that '-behaviour(Name).'  was just another wild attribute which only had a meaning to the current version of the compiler, and not part of the language proper. On the other hand, since the -callback declarations were introduced, it's pretty clear that behaviours are a language feature.
> 
> Along these lines, I think that e.g. the type declarations of a module should be included in Beam files as standard chunks, so you don't need to extract them from the "abstract format" debug information, if present, but that's another can of pull requests.
Along those exact lines, I argue that -callback attributes are nothing more than a preprocessor directive that gets transformed into behaviour_info/1 function, and therefore has no place in the standard chunks.



More information about the erlang-questions mailing list