OOPS in Elixir?

So in C++, we can create a class and then create an instance of this class in another new class like what has been done in the following:

# Creating the MyClass class
class MyClass{
    this.my_list = []
       return this.my_list.length == 0
class MaxClass{
        this.max_law = new MyClass() # Creating the MyClass instance in MaxClass
        this.main_law = new MyClass()# Creating the MyClass instance in MaxClass
  checking values(){
        result = this.max_law.is_empty() # Using the MyClass is_empty function
        return [result, this.main_law.is_empty()] # Using the MyClass is_empty function

Now, I was able to create the MyClass module like the following:

defmodule MyClass do
  defstruct my_list: []
  def init, do: %MyClass{}
  def is_empty(%MyClass{my_list: elems}), do: length(elems) == 0

I tried then creating the MaxClass module as well like the following:

defmodule MaxClass do
  def init do
    max_law = main_law = MyClass.init
   {max_law, main_law}

However, I am not sure how we can use this similar to how it is being used in C++.

I apologize for asking another weird question. The Elixir community over here has been an immense help and really glad for all the responses from you guys.

1 Like

Thinking in terms of OOP is going to slow You down.

There are other patterns in FP.

  • You should not think inheritance, but composition.
  • Data is immutable, and has no functions attached to it. When You reload the system, data is untouched. You don’t have internal mutable state. You can reload a running system.
  • If You think Class and instances, the analogy is Module and processes. You can have many concurrent processes running at the same time.
  • The only way to communicate is by message passing.

That makes the language OOP, in a way :slight_smile:


Thank you for your answer. Would you happen to have some examples for this since I’m still a little confused over what approach to use?

1 Like

This one for concurrency

This one for FP patterns