LauraBeatris

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

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

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

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

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: ...

:slight_smile:

kokolegorille

kokolegorille

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

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement