dogweather

dogweather

Heretical thoughts: OOP with Elixir

So I’m reading a tutorial and it’s showing how to rebind a variable (a style I don’t like):

person = Map.put(person, :region, "west-1")

And it struck me how much it looks like de-sugared Python. I.e., with a theoretical OOP syntactic sugar layer, we could do this:

person.put(:region, "west-1")

IMO, that’s more concise and expressive. And the OOP syntax would simply the search for compatible functions. E.g., it could combine both List and Enum for lists.

So what are the arguments against this kind of object-oriented syntax? Is rebinding a slippery slope to difficult-to-understand code? What are the strengths of the more repetitive functional syntax? Is it the change in how we think about data and functions as separate?

Most Liked

gregvaughn

gregvaughn

I don’t see anything Object Oriented in your question. It seems you want mutable data structures. Immutability is expected throughout BEAM code.

al2o3cr

al2o3cr

IMO the person.put syntax is deceiving, it can’t really “mutate” the thing it looks like it should. For instance (hypothetical, won’t compile):

person = %{region: "east-1"}
also_person = person
person.put(:region, "west-1")

# what is also_person.region?

person and also_person start out referring to the same Map, but then person is changed to refer to a different, updated Map. An OO-style mutation would result in also_person.region changing as well.

You could get close as-is with pipes and import:

import Map

person =
  person
  |> put(:region, "west-1")

or even (with the two-argument form of put_in):

person = put_in person[:region], "west-1"

The pipe syntax tends to lead to a lot of the “de-sugared Python” flavor, since it threads through the first argument.

Also worth reading the old discussions around “tuple calls” (Tuple Calls) which covers some similar conceptual space.

peerreynders

peerreynders

Mutation

person.put(:region, "west-1")

vs. new value

person = Map.put(person, :region, "west-1")

at least in my mind.

Where Next?

Popular in Discussions Top

mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
New
jeramyRR
This is an interesting article to read. Elixir’s performance, like usual, is excellent. However, it seems like the high CPU usage is co...
New
Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New
crabonature
I’m still quite new to Elixir. As I understand we got in Elixir “multi guards” as convention to simplify one large guard with or’s?: de...
New
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
Qqwy
I would like to spark a discussion about the static access operator: .. For whom does not know: it is used in Elixir to access fields of...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
joeerl
I’m playing with Elixir - It’s fun. I think @rvirding does give Elixir courses these days. Re: files and database - when I given Erlang ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Other popular topics Top

hakarabakara1
I have three modules, a Business which is associated Tags and Categories though join tables business_tags and business_categories. I inte...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement