Reputation: 168
As the title, does anybody could explain the use of parse_transform with ms_transform? what the different between with it and without it ?
Upvotes: 2
Views: 647
Reputation: 20916
The -compile({parse_transform, ms_transform}).
syntax invokes a parse transform.
A parse transform is a module which the compiler calls after the file or input has been parsed. The module is called with the full abstract syntax of the whole module and must return a new abstract for a whole module. The parse transform is allowed to do whatever it wants as long as the result is legal erlang syntax. It is like a super macro facility which works on the whole module not just single function calls. The resulting module is then compiled. You can have many parse transforms.
Parse transforms are typically used to do compile-time evaluation and code transformations. The ets:fun2ms
call mentioned by @P_A is a typical example of this as it takes a fun and at compile-time transforms this into a match specification, see Matchspecs and ets:fun2ms. But parse transforms allow you to do much more, for example add and remove functions. An example of this is a parse transform which generates access functions for all the fields in a record.
It is a very powerful tool, but unfortunately easy to get wrong and so create a real mess. There are, however, some 3rd party support tools which can be very helpful.
Upvotes: 7
Reputation: 1818
ms_transform
module implements parse_transform that translates fun syntax into match specifications. For example ets:fun2ms
fun uses it.
Also you can use
-include_lib("stdlib/include/ms_transform.hrl").
Upvotes: 1