LauraBeatris
What is "def" and why it has to come with "do"
First of all, I want to say sorry because it’s probably a really dumb question…
I’m starting to learn Elixir and i come from a different background (Typescript) so i’m having some difficulty understanding the syntax of modules and methods.
With that being said, what does def mean when declaring a method? And why it has to come with do in the end?
Marked As Solved
NobbZ
def means “define the funtion with name, arguments and body”.
The do is necessary to distinguish body from name and args.
In other languages curly braces are used or indentation.
Also in Elixir there are no methods, only functions.
Also Liked
jepemo
Hello, there are no methods in Elixir. The organization is “functions” inside “modules”. “def” is for “define” a function. And “do” is for “open” a code block, like “{ }” in TypeScript.
LostKobrakai
The syntax is inspired by ruby. You can compare def to public function … you see in many OO languages (not in js/ts though), while do … end are like { … }.
kokolegorille
… and to add to the previous replies, do end is a syntaxic sugar for , do:
You can write
if ... do
...
else
...
end
# as
if ..., do: ..., else: ...

kokolegorille
It surely is because the teacher comes from oop, but talking about methods is strictly prohibited here 







