io:format() vs io_lib:format()

Raimo Niskanen raimo.niskanen@REDACTED
Wed Feb 26 16:22:18 CET 2003


io:format(Format, Args) uses io_lib:format(Format, Args) to create the 
data that is written to stdout, so there should be absolutely no 
difference in the result. Apart from that io_lib:format/2 returns a deep 
string, and io:format/2 just does a side effect.

io:put_chars(io_lib:format(Format, Args)) would be exactly equivalent to 
io:format(Format, Args).

The feature in io_lib:format/2's return value is that the string need 
not be flattened if it is supposed to be sent to e.g io:put_chars/1, 
file:write/2 or erlang:port_command/2. It is a perfectly valid IO-list 
and it is wasting time to flatten it if it is not needed.

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



Bengt Kleberg wrote:
> greetings,
> 
> are the differences between io:format() and io_lib:format() documented
> anywhere? or is there no difference?
> 
> 3> io:format( "asd~s~n", ["asd"]).
> asdasd
> ok
> 4> io_lib:format( "asd~s~n", ["asd"]).
> [97,115,100,"asd","\n"]
> 
> 
> io:format() gives me a single string.
> io_lib:format() gives me a string with a string as element 3 (0 based).
> 
> is this a bug or a feature? or is it a result of flattening the string
> as it goes to stdout?
> 
> 
> bengt
> 




More information about the erlang-questions mailing list