What is the purpose of the module in Elixir?

Asked

Viewed 94 times

6

I’m studying Elixir and I came across the keyword defmodulo, I used to define a basic module that displays a message:

defmodule Mensagem do    
    def escreve(msg) do
        IO.puts msg
    end    
end

Calling in the following way:

Mensagem.escreve("Oie amigo do gato :D")

Exit:

Look friend of the cat :D

The way the module is defined reminded me a lot of a class.

Doubts

  • What is the purpose of the module in Elixir?
  • A module is the same thing as a class?
  • There may be instances of it to represent an object?
  • 1

    It seems to me that the modules in this case will not have instances, they will not be objects, the modules are closer to namespaces, as if they were functions within namespace in PHP, or defs within scopes (modules also) in Python, in fact Elixir remindedme enough as it is in Python and Ruby, the modules.

1 answer

6


What is the purpose of the module in Elixir?

Basically group functions that are related and create a scope for their use, so the functions within it can only be accessed through his name.

A module is the same thing as a class?

Not exactly, but since AP knows C# I can buy it. It’s like a static class of this language, which is not a class as you know it. The module has many limitations to establish that it is actually a class, was used an existing term in C#, but it is wrong, VB.NET also calls the same mechanism of Module.

He’s more like one namespace, despite having some different semantics.

There may be instances of it to represent an object?

No, that’s the most obvious limitation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.