六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 10|回复: 0

recbird

[复制链接]

升级  4.67%

66

主题

66

主题

66

主题

举人

Rank: 3Rank: 3

积分
214
 楼主| 发表于 2013-2-4 19:55:11 | 显示全部楼层 |阅读模式
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.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表