List as functions argument never matched

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 :innocent: 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]

Upper case is reserved for module names. Variable have to be lowercase.
If you change your function like this, it should work:

def choice(l) when is_list(l) do
		IO.puts("List")
		Enum.random(l)
	end
2 Likes

Thanks a lot!
I had a shadow of knowledge of not using Capital names, it haven’t materialized yet)

1 Like