2013-03-07 54 views
0

下面的代码确实有警告有些编译:Erlang:将记录传递给函数?

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]).  

回答

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行。)

+0

哦,对......这是我忘了的事情。谢谢! – pandoragami 2013-03-07 13:29:12

1

作为警告消息说,问题是在第8行:

io:format("~p ~p~n", [ARG1,NAME,TO_GO]) 

您正在向格式字符串传递三个参数列表:ARG1,NAME,TO_GO,但格式字符串仅使用其中两个(只有两个〜p)。它与记录无关。

+0

我将它固定在这个'io:format(“〜p〜p〜p〜n”,[ARG1,NAME,TO_GO])上。 '但我仍然遇到运行错误? 'passing_records:record_passing([#通{ARG1 = 2,名字= “X”,to_go = 5}])。 * 1:记录传递未定义' – pandoragami 2013-03-07 13:16:48