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
Wrote this guide on how to integrate DropzoneJS with Phoenix Liveview. Hope it helps someone out!
https://sergiotapia.com/dropzonejs-dir...
New
Recently we partitioned a table with 28 million rows by its ULID to improve query speeds and reduce query timeouts. To do this I ended up...
New
To integrate dropdown menus in a Phoenix Liveview app, you can use a combination of js, Hooks, CSS and your .leex and .ex code. You can...
New
So I’m really loving elixir. BY FAR the most excruciating piece of learning a functional language for me is having to “transform” all my ...
New
I wrote this blog post based on our experiences setting up continuous delivery for our first production umbrella Phoenix app. Coming from...
New
Hi everyone,
Just wanted to say that the new Self-referencing many to many guide is now up on the official Hex docs (at least I just not...
New
This blog post hit my timeline earlier, and I’ve also been learning about some fantastic Elixir related tips via @pragdave’s new online c...
New
We just published a guide to automatic clustering in Elixir 1.9, with Mix releases and libcluster.
The cluster automatically discovers n...
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
..or as and when you can think of one :icon_cool:
This thread my also be of interest: What took you way too long to figure out? :003:
New
Other popular topics
Manning 2016 Halloween weekend sale via Deal of the Day
Friday, October 28 - Half off all MEAPs - code WM102816LT
Saturday, October 29 ...
New
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
can someone please explain to me how Enum.reduce works with maps
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
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
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
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
by Lance Halvorsen
Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
New







