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

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement