AstonJ
Want OOP in Elixir?
https://github.com/wojtekmach/oop
Are you tired of all of that modules, processes and functions nonsense? Do you want to just use classes, objects and methods? If so, use OOP [1] library in Elixir [2]!
[1] Actually, according to Alan Key, the inventor of OOP, “objects” is the lesser idea; the big idea is “messaging”. In that sense, I can’t agree more with Joe Armstrong’s quote that Erlang is “possibly the only object-oriented language”.
[2] Please don’t. You’ve been warned.
import OOP
class Person do
var :name
def say_hello_to(who) do
what = "Hello #{who.name}"
IO.puts("#{this.name}: #{what}")
end
end
joe = Person.new(name: "Joe")
mike = Person.new(name: "Mike")
robert = Person.new(name: "Robert")
joe.say_hello_to(mike) # Joe: Hello Mike
mike.say_hello_to(joe) # Mike: Hello Joe
mike.say_hello_to(robert) # Mike: Hello Robert
robert.say_hello_to(mike) # Robert: Hello Mike
joe.set_name("Hipster Joe")
joe.name # => Hipster Joe
![]()
Most Liked
Qqwy
This is absolutely amazing. The next step is a Ruby cross-compiler!
All joking aside, my mind is blown by how much you can do with Elixir’s metaprogramming features. Thank you for sharing!
Qqwy
I’ve been thinking about this a lot lately. I had a discussion about this (e.g. the never-dying OOP vs FP – which is better argument.) yesterday with a colleague. My current state of mind:
- OOP and (Actor-Model-based) Functional Programming allow for a similar amount of abstraction and conciseness in programs.
- The features in (Actor-Model-based) Functional Programming (immutability, pattern-matching, real concurrency without deadlock-dangers) allow you to write programs that are much faster than the alternative in OOP-programs.
- There are a lot less ways in (AM-based) FP to shoot yourself in the foot than in OOP, because patterns like Demeters Law or tell, don’t ask resolve themselves naturally when working with processes/immutability.
I don’t remember who, but someone showed me this image a while back:
Basically, there are less patterns to learn, because most problems we face in Object-Oriented design resolve themselves in the functional world.
The hardest thing is to change your mindset from a I have this data and I want to put them in a hierarchy of objects to I have this data and I want to write functions/processes that consume it.
Of course there are nice features like Polymorphism, that some claim only exist in OOP-land, but I’d argue that that isn’t true; In Elixir we have Behaviours and Protocols to fill this gap, and they work great at allowing code-re-usage and ‘swapping out components’.
AstonJ
Popular in Discussions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









