I'm going through the Programming Phoenix book and I am wondering what the difference between def
and defp
is.
There are several functions in my controller - most of them are actions like this:
def new (conn, _params) do
...
end
The book had me create another function in this controller that is not a typical controller action like this:
defp user_videos(user) do
...
end
So my question is how do I know when to use defp
and when to use def
when defining a function inside a controller in the Phoenix Framework.
From Elixir’s documentation on functions within modules:
Inside a module, we can define functions with
def/2
and private functions withdefp/2
. A function defined withdef/2
can be invoked from other modules while a private function can only be invoked locally.
If you have more questions, feel free to read that documentation.