Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

What's better (and more flexible) than Object Oriented is Polymorphism.

The pipe operator |> that you mention is great example because it does something in OO that is typically done using a 'fluent interface'[0] often used in 'Builder' classes (i.e., the class methods return the object so that it can be chained together). The great thing about the pipe operator is that it's much more simple and flexible, it simply passes the result of the prior function as the first argument to the second. This means, unlike with a fluent API, you can pipe together functions from completely different modules:

    :crypto.hash(:md5, some_binary)
      |> :base64.encode_to_string()
Elixir also has protocols[1], which allow a function to be used across a variety of types (similar to obj-c/Swift). Which again is more flexible, as the functionality can extend a 'class' that you don't control.

Elixir also has behaviors[2], which just guarantee that a module contains a number of functions, something akin to interfaces Java.

Dialyzer can take advantage of behaviors and protocols to enforce typing constraints.

[0] https://en.wikipedia.org/wiki/Fluent_interface [1] https://hexdocs.pm/elixir/1.12/Protocol.html [2] https://hexdocs.pm/elixir/1.12/Behaviour.html



Yes! Another thing I love about FP is the simplicity of terms. So like how you mention a return value can flow through different modules, if you wanted to do something like that in OO, you’d have to come up with a concept for what the new object is (ie, Service or what have you) which in turn has to take an object, breaking the whole “data and behaviour combined” idea. In FP, you just have modules. You don’t have think much past that a module is a group of functions that work on a certain data structure and other modules might also work on that data structure and that is fine (though of course you’ll want to hide those behind another module and delegate if you are dealing with a complex model, but that is a whole other problem that also exists in OO).




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: