Hi There,
I was playing around with the Diameter example and came across a new type: improper_list. I never knew of its existence and apparently it is not intended to be passed out of internal workings to external API callers.
But the message() type that is returned is documented as -type message() :: record() | maybe_improper_list().
My use case is such that I pass Erlang data structures out via JSON string to a frontend. Of course the json:encode(..) function failed when it encoded the improper_list type, there doesnât seem to be a universally agreed upon representation of the Erlang improper list type - fair enough.
Having an unrecognised type and extending the json encoder isnât a big deal, Iâve done it for Pid, Ref, Key-Value lists etc. What I find strange is that I canât do this for improper_lists because there isnât a reliable test for improper_list, i.e., is_improper_list(...)
My argument for such a builtin is that improper lists break code that works with lists while pretending to be a list:
1> is_list([1|1]).
true
2> length([1|1]).
** exception error: bad argument
in function length/1
called as length([1|1])
*** argument 1: not a list
Of course I could create my own is_improper_list:
is_improper_list(V) when not is_list(V) ->
false;
is_improper_list(V) when is_list(V) ->
try
length(V),
false
catch
_ -> true
end.
but this breaks the anti-pattern of using exceptions as control flow, which I donât wish to be doing. Plus I donât want to be raising - potentially - exceptions for each data structure that I might or might not be sending out over a wire via a web-socket.
My explicitly use case involves not knowing exact types, so tighter type checking isnât a solution for me.
Is there a reason why is_improper_list doesnât exist? Second is there a reason for it not existing?
Cheers!
EDIT: code fix - boolean logic on a sunday afternoon doesnât work!