Reputation: 5585
The following code does compile somewhat with a warning:
23> c(passing_records).
passing_records.erl:8: Warning: wrong number of arguments in format call
{ok,passing_records}
but when I try to run it I get this error trying to pass variables to a record named pass
:
22> passing_records:record_passing([#pass{arg1=2,name="x",to_go=5}]).
* 1: record pass undefined
Heres the code:
-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]).
Upvotes: 1
Views: 1911
Reputation: 41578
The reason for the record pass undefined
error is that you need to load the record in the shell with the rr
command to be able to use it directly. See this question for more information.
When I do that, I get to the problem that the compiler is warning about:
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])
(You're also passing the record in a list, while the function expects just a record; thus the error on line 3.)
Upvotes: 5
Reputation: 10557
As the warning message says, the problem is on line 8:
io:format("~p ~p~n", [ARG1,NAME,TO_GO])
You are passing in a list of three arguments to the format string: ARG1, NAME, TO_GO, but the format string only uses two of them (there are only two ~p). It has nothing to do with records.
Upvotes: 1