Heretical thoughts: OOP with Elixir

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.

3 Likes