KoviRobi
Debug function call macro
Hi,
I’ve written the following to debug function calls, not sure if it’s useful for anyone else, and if so should I put it somewhere?
iex(1)> require Debug; import Debug
Debug
iex(2)> inspect_call(round(2.3*4.5))
round(10.35) -> 10
iex(3)> inspect_call(inspect({:foo, 1+2}))
inspect({:foo, 3}) -> "{:foo, 3}"
"{:foo, 3}"
iex(4)> inspect_call(IO.inspect(1) + IO.inspect(2))
1
2
+(1, 2) -> 3
3
It’s hopefully a fairly easily understandable piece of macro, though only evaluating the arguments once did make it more complicated. The first pattern-matching is taken from the Macro.decompose_call/1 function.
defmodule Debug do
defmacro inspect_call(block) do
{name, args, rebuild} =
case block do
{:{}, _metadata_a, args} when is_list(args) ->
raise CompileError,
description: "Using inspect_call on a map literal is not supported",
file: __CALLER__.file,
line: __CALLER__.line
{{:., metadata_a, [remote, function]}, metadata_b, args}
when is_tuple(remote) or is_atom(remote) ->
{
Macro.to_string({{:., metadata_a, [remote, function]}, [no_parens: true], []}),
args,
fn new_args -> {{:., metadata_a, [remote, function]}, metadata_b, new_args} end
}
{name, metadata_a, args} when is_atom(name) and is_atom(args) ->
{to_string(name), [], fn _new_args -> {name, metadata_a, args} end}
{name, metadata_a, args} when is_atom(name) and is_list(args) ->
{to_string(name), args, fn new_args -> {name, metadata_a, new_args} end}
_ ->
raise CompileError,
description:
"Cannot understand function call #{Macro.to_string(block)}",
file: __CALLER__.file,
line: __CALLER__.line
end
eval_args =
for n <- 1..length(args) do
Macro.unique_var(:"eval_arg_#{n}", __MODULE__)
end
return_value = Macro.unique_var(:return_value, __MODULE__)
quote do
unquote_splicing(
for {e_a, a} <- Enum.zip(eval_args, args) do
quote do
unquote(e_a) = unquote(a)
end
end
)
IO.write(
unquote(name) <>
"(" <>
(unquote(eval_args)
|> Enum.map(&inspect/1)
|> Enum.join(", ")) <>
")"
)
unquote(return_value) = unquote(rebuild.(eval_args))
IO.puts(" -> " <> inspect(unquote(return_value)))
unquote(return_value)
end
end
end
Most Liked
sabiwara
Elixir Core Team
Good news: starting from Elixir 1.15, prying for dbg in IEx becomes opt-in
.
5
hauleth
There is already such macro in Kernel
2
Popular in Guides/Tuts
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
So my main OS is Windows, I do must of my work with it, Elixir and vscode elixirls works just fine when you’re working only with elixir, ...
New
Me and my boys started a new website specifically designed for other ASP.NET programmers that struggle, as we do, with their transformati...
New
Another cool plugin for Neovim, GitHub - jmbuhr/otter.nvim: Just ask an otter! 🦦 · GitHub makes it possible to run linters for embedded c...
New
In a quest to optimize the amount of data sent between the server and client I recently decided to try to use MessagePack instead of JSON...
New
I Created a blog post about setting up svelte with phoenix.
I found it a bit tricky and the only blog post I found was written using som...
New
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
We finally have a Mix clustering guide to go with Phoenix deployment with Mix Releases.
Deploy an Elixir Cluster with Mix Releases and l...
New
Just a quick heads up: There seems to be a bug in Erlang/OTP 21.3, which can cause some errors when making http requests. If you’re using...
New
When we were figuring out how to use Phoenix LiveView we got stuck a few times.
So in order to save other people time, we created a comp...
New
Other popular topics
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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 am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
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
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something…
Haskell reminds me of Java, and e...
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








