fceruti
Sup ppl, I just wanted to share a little macro I created to simplify LiveView code. It allows you to convert
socket.assings.variable
into
~a|variable|
For instance here’s a function from Chris McCord’s live_trek
def handle_event("delete", %{"id" => id}, socket) do
todo = Todos.get_todo!(socket.assigns.scope, id)
{:ok, _} = Todos.delete_todo(socket.assigns.scope, todo)
{:noreply, socket}
end
Would be
def handle_event("delete", %{"id" => id}, socket) do
todo = Todos.get_todo!(~a|scope|, id)
{:ok, _} = Todos.delete_todo(~a|scope|, todo)
{:noreply, socket}
end
Necessary? Not really.
But I thought it looks much better, specially when you have lot’s of socket.assigns and want to reduce verbosit, avoid unpacking assigns and reduce formatter line breaks.
Here’s v0.1
defmacro sigil_a(expr, _modifiers) do
socket = Macro.var(:socket, nil)
quote do
Map.get(unquote(socket).assigns, String.to_existing_atom(unquote(expr)))
end
end
And here’s a vim command to search and replace:
:%s/socket\.assigns\.\([a-zA-Z0-9_]*\)/\~a|\1|
Trending in Guides/Tuts
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
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
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 10 of 13 Posts
tfwright
Nice idea. Curious, why Map.get rather than fetch? I think the behavior being replaced raises if the expected key is missing, whereas this will return nil I think
fceruti
No reason, probably a bug
ityonemo
zachallaun
If you wanted to be really evil, you could overload the
@operator if the caller environment containssocket(don’t do this)
v0idpwn
This would have a lot of potential if there were good overridable unary operators in Elixir.
Unfortunately,
.
+,-,!,^,not,@,&are too useful and meaningful to be overriden. There is~~~but its ugly and too hard to writeMakes me think Elixir maybe could support an unary
$operator.ityonemo
I mean, it’s what happens in EEx templates, though!
cmo
$is being used for the potential new type systemtheodore
Pretty cool!
sodapopcan
Innnnnteresting. I was trying to figure out how to do something like this and couldn’t figure it out. I was trying to use
var!/2for this to no avail. I never understood that you can check the caller env.I was looking to create a single guard that would work on both
socketandassigns. I have a lot of single module CRUD LiveViews so I wanted something like:This would check whichever existed of
socket.assigns.live_actionorassigns.live_actionwas in[:edit].Even though I don’t think that is particularly evil (that is, what I wanted to do—what you’re suggesting is absolutely pure evil
) , I ultimately decided I’d rather not do anything out the ordinary like this so I never bothered to seek help figuring it out, but it’s nice to know how it could be done, so thanks!
(that is, if that would even solve the problem… I haven’t actually tried, just responded immediately, lol, but still informative for me)
zachallaun
Glad it was informative!
It’s definitely worth digging into the environment info you have available in macros. Even in the OP’s example, you could check the caller env for a socket var and raise an informative error if it doesn’t exist (as opposed to the underlying Map.fetch! error or whatever you end up using).