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
Popular in Discussions
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
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
I wanted to capitalize a string, and tried using String.capitalize().
That generally works well, until you try to capitalize a word like...
New
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...
New
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
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...
New
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
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
Looking at the stacks that existing large companies have used, WhatsApp internally uses Mnesia to store the messages, while Discord uses ...
New
Hi all
How can I compare haskell with elixir, included tools, webservices, ect.
Thanks
New
Other popular topics
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
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
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
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
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
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
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
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
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
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
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









