5 ms·
> In real life, we don’t need to write code with receive do loops. Instead, we use one of the behaviours created by people much smarter than us. I make more th
by pmontra 6y ago
> In real life, we don’t need to write code with receive do loops. Instead, we use one of the behaviours created by people much smarter than us.
I make more than half of my income from Elixir. That said,
the naive receive loop is much easier to understand than any of the GenServer examples. They pollute the module logic with all that handle_* boilerplate. I believe that neither Erlang nor Elixir got the right abstraction there. Method definition in any OO language is easier to understand. At least Elixir should have taken Agent and made it an invisible part of the language. All those calls to Agent in this example https://elixir-lang.org/getting-started/mix-otp/agent.html https://elixir-lang.org/getting-started/mix-otp/agent.html are still boilerplate.
- jeremyjh 6y agoThere are libraries that do this, like https://github.com/sasa1977/exactor https://github.com/sasa1977/exactor but the Elixir community nowadays seems more conservative about macro "magic" than it was a few years ago. Sasa has written an entire book about OTP and if he doesn't recommend using his own library I wouldn't argue with him, but I haven't written a Genserver myself in years despite writing Elixir for a living the past 3.5 years. If you are going to learn how they work though, it makes sense to stick to how Erlang actually implements them.
- buzzerbetrayed 6y ago> There are libraries that do this, like https://github.com/sasa1977/exactor https://github.com/sasa1977/exactor At the very top of the Extractor readme: > I don't maintain this project anymore. In hindsight, I don't think it was a good idea in the first place. I haven't been using ExActor myself for years, and I recommend sticking with regular GenServer instead :-) This both confirms what you said about the Elixir community shying away from macro magic, and makes this library a very bad idea to use in new projects, imo.
- jeremyjh 6y agoRight, thats why I said he doesn't recommend using his own library. My point is people have been down this road, and moved away from it, not that anyone should use it.
- deleted 6y ago[deleted]
- dqv 6y agoDo you do that with proc_lib or is it just a completely otp-unaware process like Joe would do?
- filmor 6y agoOf course it's easier to understand. It's also much easier to get wrong. Just yesterday I messed around with running a small process on to ship subscribed messages across node boundaries and forgot the recursive call in one of 4 cases, leading to mysterious stopping. In Elixir you don't even have to define all of this boilerplate, just the functions you actually need. Also, the callbacks are not classical methods, a handle_call doesn't have to reply immediately, for example. Distinguishing info, call and cast is also very relevant. If you wanted something like {:ok, agent} = Agent.start_link ... agent.get() I think you would need a change in the BEAM to recognise that agent is a "particular kind of PID".
- pmontra 6y agoThat's what I was thinking about, client side. Probably the Elixir compiler could detect it and generate the code to bridge the gap. It feels OO-ish but we're dealing with mutable state anyway. I prefer "easy" than "pure".
- thelazydogsback 6y agoSeems like you're losing something here -- ISTM that the goodness of a reified massage is now lost. A message, as a piece of data, can be tested, stored, passed on, etc., while a "call" (the .get()) cannot.
- tlack 6y agoWhat you said is rarely stated in public, but I've often felt it too: OTP obscures the underlying beauty of the Erlang platform. OTP is well engineered of course, but the basic notion of the spawn -> receive -> loop cycle is so clean and illuminating that I wish newbies would hold out before learning OTP sometimes. It's natural to think of case-specific abstractions around the primitives that are more germane to the domain at hand than OTP's.
- dnautics 6y agoYeah but that's like saying the awkwardness of stl templates obscures the beauty of C++ or the Android app api obscures the beauty of the jvm. The genserver is messy because it deals with a good chunk of messiness around safely managing distributed systems for you. It probably could be improved (Dave thomas critiques comes to mind) but elixir had to be conservative because if it weren't it wouldn't have had buy in from the beam community and wouldn't have become the first truly successful non-erlang language targeting the BEAM.
- probotect0r 6y agoI finished reading Elixir in Action a few days ago. This was probably the best part of that book. It takes you through building a primitive GenServer with basic processes before moving onto actually using GenServer. It specifically goes over tail recursion optimization before it introduces the receive do loop, which seems like a very important part of the whole thing.
- AlchemistCamp 6y agoElixir in Action is fantastic. Its approach is so different from the other introductory books, but the payoff is real.
- arusahni 6y agoWould you recommend the book?
- bostonvaulter2 6y ago