record_info不能接受參數?

2009-04-10
Q:有如下函數:
1> rd(test, {key, value}).
2> Arg = test.
3> record_info(fields, test).
[key, value]
4> record_info(fields, Arg).
* 1: illegal record info

Arg跟記錄名test系一樣的,為什么传入record_info里會提示错误呢??

A:转Google Goups - [erlang-questions]上的一段解答:
record_info is not a proper function. It only exists during compilation, which means that it cannot take variable arguments:

record_info(fields, address) %% works
Rec = address,
record_info(fields, Rec) %% doesn't work!

Of course it only works if the address record is defined in that module. If you do want a function that uses record_info, try to use a macro instead.

定義成Macro就可以?馬上試試先:
-record(test, {key, value}).
-define(FIELDS(Record), record_info(fields, Record)).
....
?FIELDS(test).
> [key, value]

哇哇,果然OK哦~~

M-OSCAR | Powered by Blogger | Entries (RSS) | Comments (RSS) | Designed by MB Web Design | XML Coded By Cahayabiru.com