Mrewan79

Mrewan79

How do I use protocols to implement an OOP type abstract class?

Apologies if this question has already been asked.

As I understand it, the way to translate a concept similar to use of abstract classes in Elixir is to use Protocols.

For instance if I have an abstract class of “Item” and I want it to be inherited for instance into “Weapon” or “Food” I would like to utilise the methods and values of this class. I do not want the parent methods to be accessed directly, but wish to add additional methods and utilise the child methods in their own way. (i.e. weapons can do damage, food is edible, weapons aren’t).

I can see the documentation here; https://elixir-lang.org/getting-started/protocols.html but am still not totally clear. The examples seem to work like overrides.

Marked As Solved

CptnKirk

CptnKirk

Protocols aren’t about inheriting data fields, they expose standardized behavior across data types. Think about polymorphic methods in OO languages.

You have a Shape, Circle, Square, Triangle classes. Maybe Shape is an interface or abstract class. It defines certain methods common to the shape family of classes that each subtype must implement. For example, an area() method. Each Shape is able to compute its own area, however, those calculations differ depending on subtype.

With Protocols you can do something similar. You can define a Shape protocol along with protocol implementations for each Square, Circle, Triangle struct you might have. These protocol implementations could be defined within the given modules, or supplied externally in another module. The point is, you can have a function operate on data for which there is a Shape protocol implementation in the same way, regardless of the actual underlying contents of the data.

This is how the Enum module provides standard functionality for processing Enumerable things. Enumerable is a protocol that defines four functions. If you define a protocol implementation that implements those four functions for your data, then you can leverage the Enum module for your data as well.

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Can you perhaps talk more about the problem you’re trying to solve? The questions you’re asking are sort of the wrong questions in the paradigm that Elixir is. There are no classes, abstract or otherwise, no methods and definitely no parent methods.

If you can talk about about the data you want to represent, and the kinds of functionality you want, we might be able to suggest approaches that better suite a functional paradigm.

Where Next?

Popular in Questions Top

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
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement