Why are some parameters in Elixir commented out?

Hello,

The other day while I was working on an app. I kept coming across functions with parameters that have commented out with an underscore for Ex:

def mount(_params, _session, socket) do 
memo.subscribe()
{:ok, assign)
}
end

why not bother adding these in if they don’t do anything.

The framework machinery that interacts with that function requires it to accept 3 arguments, but the _ prefix tells the compiler that this head doesn’t actually use some of them.

2 Likes

They do stuff, you just haven’t built the stuff yet!

1 Like

In addition: it’s the same as mount(_,_,socket). However, giving them a name makes it easier to see what could be used in the function if ‘uncommented’.

Just to be clear to newcomers: he is kidding. It’s perfectly finished code to have a function with two clauses, for which an argument is disabled in one as it is unused.

# prefix is not used
def prefix(input, _prefix = nil), do: input 

#prefix is used
def prefix(input, prefix), do: prefix <> input

Oh I see now.

Thank you