DEV Community

Muhammed H. Alkan
Muhammed H. Alkan

Posted on

What does Elixir defmodule actually?

I want to know why defmodule exists in Elixir. What does it do actually?

Top comments (5)

Collapse
 
allanmacgregor profile image
Allan MacGregor πŸ‡¨πŸ‡¦

Muhammed,

Hard to answer the question since you don't elaborate clearly, have to read the documentation and after doing so is that explanation not clear?

If you haven't done so start here:

elixir-lang.org/getting-started/mo...

and

hexdocs.pm/elixir/Kernel.html#defm...

Collapse
 
lyfolos profile image
Muhammed H. Alkan

Simply, I just asked why defmodule exists in Elixir unlike other languages.

Collapse
 
allanmacgregor profile image
Allan MacGregor πŸ‡¨πŸ‡¦

Muhammed,

You missed the point I tried to make on my answer, I was trying to point you are not asking a good enough question, and you will clearly not get a good enough answer.

You tried to clarify saying why does this piece of syntax exist in elixir and not in other languages, which is still not a good question, but let's elaborate.

Briefly let's take Ruby as an example as Elixir takes heavy inspiration from Ruby in terms of syntax. In ruby we have a module macro, so you can do something like

module Dog 
   def talk
      puts "Woof!"
   end
end 

Same code in Elixir would be fairly similar minor difference, now if what you are actually trying to get at is that you want an explanation of what a module is and how is used, thats a very different thing:

Modules are similar to classes in other languages, and are used to structure code cleanly; defmodule is just a language macro for defining a module.

I would strong recommend going through elixirschool.com/en/lessons/basics... from scratch if you are fairly new to the language or programming in general.

Thread Thread
 
lyfolos profile image
Muhammed H. Alkan

I'll start soon to Elixir, btw thanks.

Collapse
 
lyfolos profile image
Muhammed H. Alkan

Thanks!