Qqwy

Qqwy

TypeCheck Core Team

How to do polymorphism in a process-based world

I am getting more and more used to writing things in Elixir. However, when trying to create intermediate applications that use multiple processes, my years of working with Object-Oriented Programming languages kick in, wanting to create processes that re-use some of the logic of other processes. In other words: Some kind of polymorphism.

Say I have a type of GenServer called Vehicle. This Vehicle has a function called drive(pid, kilometers), which modifies the given vehicle process’s state, to keep track of the total kilometers driven.

I also have a GenServer called Car, also having a drive/2 function. Car behaves in exactly the same way as Vehicle, except that the amount of kilometers driven also subtracts some of the gasoline amount left in the cars fuel tank.

How is functionality like above properly constructed in Elixir? How can I re-use things defined in Vehicle for Car?

Marked As Solved

alamba78

alamba78

You should probably look into Elixir Protocols.

Also Liked

mkunikow

mkunikow

I say In FP you do everything by operate on pure functions, functions composition, higher order functions.
So for example you can have higher order function drive-common with common logic for drive that has as input parameter lower order function with logic specific to Car car-specific or Vehicle vehicle-specific.
pseudo code:

drive-for-car = drive-common(car-specific)
drive-for-vehicle = drive-common(vehicle-specific)

PS
Processes are not the same as objects in OOP and I will say they has nothing to do with polymorphisms.

fomt http://blog.cleancoder.com/

Qqwy

Qqwy

TypeCheck Core Team

To clarify: I totally understand that processes are not the same thing as objects from an OOP-language. (Although some might argue that they are closer to what Alan Kay envisioned).

I am just wondering how to properly re-use code (i.e. avoiding code-duplication), and how to talk to two different but similar types of processes properly.

Last Post!

Qqwy

Qqwy

TypeCheck Core Team

Thank you. Protocols indeed seem to be what I was looking for.

Where Next?

Popular in Questions Top

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 records...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
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
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

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
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

We're in Beta

About us Mission Statement