List as functions argument never matched

rrrr
Thu, 02 Mar 2023 12:24:24 +0000
Questions / Help

I got 2 warnings and “unexpected” behavior when trying to match argument as a list. Is this code incorrect? How can I match a argument as a List?
(It’s first code i am writing by myself I was trying to be close to the “book”)

I have following code

	def choice(L) when is_list(L) do
		IO.puts("List")
		Enum.random(L)
	end

	def choice(a \\ -1, b \\ 1) do
		IO.puts("2 params")
		Enum.random([a,b])
	end

Compiling warnings as following:

warning: this check/guard will always yield the same result
NF.exs:2

warning: incompatible types:

    L !~ [dynamic()]
in expression:

    # NF.exs:2
    is_list(L)

Results of executions are strange to me, it seems only last function is matched
2 params
1
…
2 params
[-2, 2]
…

3 posts - 2 participants