[erlang-questions] io_lib format inconsistancies

Andrew Thompson andrew@REDACTED
Fri Sep 7 21:03:29 CEST 2012


I wrote a quickcheck test for lager to compare its formatting to io_lib,
and I found a bunch of dissimilarities. However, a couple of the things
I found didn't make a lot of sense, so I figured I'd ask here for an
explanation.

First one:

1> io:format("~P", [<<>>, 1]).
<<>>ok
2> io:format("~W", [<<>>, 1]).
<<...>>ok
3> io:format("~P", [<<1>>, 1]).
<<...>>ok
4> io:format("~W", [<<1>>, 1]).
<<...>>ok

As you can see, ~P and ~W behave differently on the empty binary, but
behave the same when the binary isn't empty. Seems a bit weird.

The other one that bothers me:

16> io:format("~P", ['hello world', 2]).
'hello world'ok
17> io:format("~P", ["hello world", 2]).
"hello world"ok
18> io:format("~P", [<<"hello world">>, 2]).
<<"hell"...>>ok
19> io:format("~W", ['hello world', 2]).
'hello world'ok
20> io:format("~W", ["hello world", 2]).
[104|...]ok
21> io:format("~W", [<<"hello world">>, 2]).
<<104,...>>ok

Why does a 'depth' of 2 mean we print the <<>> and the first *four*
characters of a binary? ~W behaves much more logically. Also why does
depth not apply to printable lists, but it does to binaries?

I've got a branch now for lager to be compatible with this madness, but
I wondered if anyone knows *why* things are like this?

https://github.com/basho/lager/pull/77

Andrew



More information about the erlang-questions mailing list