recbird - An Erlang Dynamic Record Inferring Parse Transform

You should have read Yariv's recless blog, a nice blog talking about how to make record accessing simple.

Recless is a static type inferring record parse transform, that means, as described in Yariv's blog:

one main restriction in Recless’s type inference algorithm: function parameters must indicate their record types for type inference to work on them. For instance, this won’t work:
get_name(Person) -> Person.name.
Instead, you must write this:
get_name(Person = #person{}) -> Person.name.

How about a dynamic record inferring solution? I got some idea that was also inspired from my friend Haobo. As I'm familiar with Erlang AST tree when developing ErlyBird, I took a try and got some good result. I named it recbird.

The magic behind recbird is, it parses the Erlang AST tree, and adds some setter/getter functions for each record's field. Then, at runtime, it will detect the first element of record var, and thus knows which setter/getter function should be redirected, and call it.

It just works now, with none limits, you can write R.a.b.c and R.a.b.c = Sth almost every where.

Notice: There may be some bugs.

The perfomance is also reasonable, for example, when you do R.a.b = 'yes' 1,000,000 times, the original Erlang record syntax takes about 300ms in my machine, the recbird is about 310ms. When you run it 10,000,000 times, the recbird is about 150% more time costed than original Erlang record accessing.

The recbird's source code can be got at: recbird.erl

To use it, compile it, include compiled recbird.beam under your code path, add

-compile({parse_transform, recbird}).

in your source file.

你可能感兴趣的:(C++,c,erlang,Blog,idea)