Mrewan79

Mrewan79

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.

Showing Posts 1 to 4

Mrewan79

Mrewan79 OP

I used the search tool and nothing came up, but that doesn’t mean the question hasn’t been asked.

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.

Mrewan79

Mrewan79 OP

#Abstract data examples

#Item - Abstract values
Weight - Int
Size - Int
Name - String
Description - String

#Food
Weight
Size
Name
Description
Edible = true

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews