abetkin

abetkin

Decorators/wrappers for Elixir the way you've never seen it!

Hi there!

From what you know, decorators in Elixir can be implemented, but it gets cumbersome and you shouldn’t do it. Below you will see a new approach that is very simple. It is not exactly decorators (the module attributes syntax is not used) but a decent replacement for them: wrappers.

defmodule Wrapper do
  defmacro apply(call, [do: block]) do
    {wrapper, _opts, args} = call
    func = makefun(do: block)
    {wrapper, [], [:wraps, func] ++ args}
  end

  def makefun(do: block), do:
    {:fn, [], [{:->, [], [[], block]}]}

end



defmodule Ex do
  require Wrapper

  # a wrapper that increments each of 2 values returned
  def incr(:wraps, f, x, y) do
    # x and y are unused here
    {a, b} = f.()
    {a+1, b+1}
  end

  def f(x, y) do
    incr(x, y) |> Wrapper.apply do
      {x, y}
    end
  end

  # a convenience macros for incr wrapper
  # just showing it can have the same name (incr)
  defmacro incr(x,y, do: block) do
    {wrapper, _opts, _args} = quote do Ex.incr end
    func = Wrapper.makefun(do: block)
    {wrapper, [], [:wraps, func, x, y]}
  end
  def g(x, y) do
    incr(x, y) do
      {x, y}
    end
  end

end

Where Next?

Popular in Discussions Top

PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
Jayshua
I recently came across the javascript library htmx. It reminded me a lot of liveview so I thought the community here might be interested....
New
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
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 39523 209
New
pillaiindu
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
New
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 14049 124
New
ejpcmac
I have discovered Nix last month and I am currently on my way to migrating to it—both on macOS at home and the full NixOS distrubution at...
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
Qqwy
Looking at the stacks that existing large companies have used, WhatsApp internally uses Mnesia to store the messages, while Discord uses ...
New
kostonstyle
Hi all How can I compare haskell with elixir, included tools, webservices, ect. Thanks
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement