dev-resources.site
for different kinds of informations.
Enhancements to dbg in elixir 1.18
Published at
12/22/2024
Categories
elixir
Author
dkuku
Categories
1 categories in total
elixir
open
Author
5 person written this
dkuku
open
Elixir 1.18 added some interesting, features but one that went under the radar was extended support for dbg
. In 1.17 when you had this code
a = 1
b = 2
if a + b == 3 do
:equal
else
:non_equal
end
|> dbg
It evaluated to:
if a + b == 3 do
:equal
else
:non_equal
end #=> :equal
In 1.18 this was aligned with what case
and cond
does:
Case argument:
1 + 2 #=> 3
Case expression (clause #1 matched):
case 1 + 2 do
3 -> :equal
_ -> :non_equal
end #=> :equal
if
result in 1.18:
If condition:
a + b == 3 #=> true
If expression:
if a + b == 3 do
:equal
else
:non_equal
end #=> :equal
if this is not enough you can wrap your code in brackets and pass it to dbg
(
input = %{a: 1, b: 2}
c = Map.get(input, :c, 20)
if input[:a] + c == 3 do
:equal
else
:non_equal
end
)
|> dbg
results in displaying results for every expression
Code block:
(
input = %{a: 1, b: 2} #=> %{a: 1, b: 2}
c = Map.get(input, :c, 20) #=> 20
if input[:a] + c == 3 do
:equal
else
:non_equal
end #=> :non_equal
)
Last missing piece is the with
macro which is sometimes a nightmare to debug, in elixir 1.18 we have support for that:
with input = %{a: 1, b: 2},
{:ok, c} <- Map.fetch(input, :c),
3 <- input[:a] + c do
:equal
else
_ -> :non_equal
end
|> dbg
The result here is:
With clauses:
%{a: 1, b: 2} #=> %{a: 1, b: 2}
Map.fetch(input, :c) #=> :error
With expression:
with input = %{a: 1, b: 2},
{:ok, c} <- Map.fetch(input, :c),
3 <- input[:a] + c do
:equal
else
_ -> :non_equal
end #=> :non_equal
This should make your debugging journey easier.
If you are stuck in an older elixir versions you can install a package that backports this functionality dbg_mate
Thanks for your attention.
elixir Article's
30 articles in total
A RAG for Elixir in Elixir
read article
Enhancing Elixir Development in LazyVim: Quick Documentation Access by Telescope
read article
Learning Elixir: Control Flow with If and Unless
read article
Pseudolocalization in Phoenix with gettext_pseudolocalize
read article
The Journey of Optimization
read article
How to use queue data structure in programming
read article
Phoenix LiveView, hooks and push_event: json_view
read article
🥚 Crack Open These 20+ Elixir Goodies
read article
Learning Elixir: Understanding Atoms, Booleans and nil
read article
Unlocking the Power of Elixir Phoenix and Rust: A Match Made for High-Performance Web Applications
read article
Elixir: Concurrency & Fault-Tolerance
read article
Enhancements to dbg in elixir 1.18
currently reading
Learning Elixir: Working with Strings
read article
Leverage ETS for Shared State in Phoenix
read article
Elixir em Foco em 2024
read article
Building HTTP/JSON API In Gleam: Introduction
read article
Phoenix
read article
Sql commenter with postgrex
read article
Learning Elixir: Understanding Numbers
read article
For loops and comprehensions in Elixir - transforming imperative code
read article
Phoenix LiveView is slot empty?
read article
Customizing IEx: Personalizing Your Elixir Shell Environment
read article
Masters of Elixir: A Comprehensive Collection of Learning Resources
read article
Leveraging GenServer and Queueing Techniques: Handling API Rate Limits to AI Inference services
read article
Chekhov's gun principle for testing
read article
Solving Advent Of Code 2024 on a elixir project
read article
Bridging the Gap: Simplifying Live Component Invocation in Phoenix LiveView
read article
Harness PubSub for Real-Time Features in Phoenix Framework
read article
Debugging with dbg: Exploring Elixir's Built-in Debugger
read article
New to dev.to and Excited to Share ProxyConf: My Elixir-Powered API Control Plane
read article
Featured ones: