以下代码在某种程度上编译并带有警告:
23> c(passing_records).
passing_records.erl:8: Warning: wrong number of arguments in format call
{ok,passing_records}
但是当我尝试运行它时,尝试将变量传递给名为pass
的记录时出现此错误:
22> passing_records:record_passing([#pass{arg1=2,name="x",to_go=5}]).
* 1: record pass undefined
下面是代码:
-module(passing_records).
-export([record_passing/1]).
-record(pass, {arg1 ,
name="",
to_go=0}).
record_passing( #pass{arg1 = ARG1, name = NAME, to_go = TO_GO}) ->
io:format("~p ~p~n", [ARG1,NAME,TO_GO]).
答案 0 :(得分:4)
record pass undefined
错误的原因是您需要使用rr
命令在shell中加载记录才能直接使用它。有关详细信息,请参阅this question。
当我这样做时,我遇到了编译器警告的问题:
Eshell V5.9 (abort with ^G)
1> c("/tmp/passing_records", [{outdir, "/tmp/"}]).
c("/tmp/passing_records", [{outdir, "/tmp/"}]).
/tmp/passing_records.erl:8: Warning: wrong number of arguments in format call
{ok,passing_records}
2> rr(passing_records).
[pass]
3> passing_records:record_passing([#pass{arg1=2,name="x",to_go=5}]).
** exception error: no function clause matching
passing_records:record_passing([#pass{
arg1 = 2,name = "x",
to_go = 5}]) (/tmp/passing_records.erl, line 7)
4> passing_records:record_passing(#pass{arg1=2,name="x",to_go=5}).
** exception error: bad argument
in function io:format/3
called as io:format(<0.24.0>,"~p ~p~n",[2,"x",5])
(你也在列表中传递记录,而函数只需要一条记录;因此第3行的错误。)
答案 1 :(得分:1)
正如警告信息所示,问题出在第8行:
io:format("~p ~p~n", [ARG1,NAME,TO_GO])
您将三个参数的列表传递给格式字符串:ARG1,NAME,TO_GO,但格式字符串仅使用其中两个(只有两个~p)。它与记录无关。