Hi all, I have an issue with a module I’m writing.
In this module I have a function called SSHLogClient.stream/1
that I’m trying to inject default values into. Ideally, I’d like to use a map just to get some structure without worrying about order (and without extra boiler plate like with Keyword.get/3
), however I’m running into a strange issue. When using map, the function just hangs… but when using a Keyword list, it returns correctly.
Can anyone explain the behavior? Or can anyone suggest a standard way to add structure to the function signature without adding too much extra code?
Below I have reproduced the behavior in the iex repl.
Working Keyword List Version
defmodule SSHLogClientKeywordList do
def stream(user: user, ip: ip, file_path: file_path) do
stream(user: user, ip: ip, file_path: file_path, offset: 0, limit: 1000)
end
def stream(user: user, ip: ip, file_path: file_path, offset: offset) do
stream(user: user, ip: ip, file_path: file_path, offset: offset, limit: 1000)
end
def stream(li = [user: user, ip: ip, file_path: file_path, offset: offset, limit: limit]) do
IO.inspect(li)
end
end
# test that the keyword list has default values
SSHLogClientKeywordList.stream(user: "user", ip: "ip", file_path: "file_path") == [user: "user", ip: "ip", file_path: "file_path", offset: 0, limit: 1000]
# true
Not Working Map Version
defmodule SSHLogClientMap do
def stream(%{user: user, ip: ip, file_path: file_path}) do
stream(%{user: user, ip: ip, file_path: file_path, offset: 0, limit: 1000})
end
def stream(%{user: user, ip: ip, file_path: file_path, offset: offset}) do
stream(%{user: user, ip: ip, file_path: file_path, offset: offset, limit: 1000})
end
def stream(li = %{user: user, ip: ip, file_path: file_path, offset: offset, limit: limit}) do
# This inspect never is called
IO.inspect(li)
end
end
# test that the map has default values
SSHLogClientMap.stream(%{user: "user", ip: "ip", file_path: "file_path"}) == %{user: "user", ip: "ip", file_path: "file_path", offset: 0, limit: 1000}
# does not resolve and hangs
iex --version
IEx 1.15.4 (compiled with Erlang/OTP 26)