I have an empty list, and, in a special case, want to append a tuple to the list. How can I do that?
I tried several approaches:
case ReqFilePath of
"style.css" ->
ResponseHeaders = [{"Content-Type", "text/css"}];
_Else ->
ResponseHeaders = []
end,
case filelib:is_file(File) of
true ->
{ok, Content} = file:read_file(File),
{output, Content, ResponseHeaders}; % Complains ResponseHeaders is not safe
false ->
not_found
end.
This does not work either, since the variable is already set. When I initialize ResponseHeaders = [] first, and then try to add a value to it
ResponseHeaders = lists:append(ResponseHeaders, [{"Content-Type", "text/css"}]);
I get a match error. How do you do this normally in Erlang?
The usual way to do this in erlang is just to use another variable:
Or you could make a function that builds the ResponseHeaders for you:
Don’t forget to change your mindset to the erlang way! 😉