Logo

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
Enhancements to dbg in elixir 1.18

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
Enter fullscreen mode Exit fullscreen mode

It evaluated to:

if a + b == 3 do
  :equal
else
  :non_equal
end #=> :equal
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

if result in 1.18:

If condition:
a + b == 3 #=> true

If expression:
if a + b == 3 do
  :equal
else
  :non_equal
end #=> :equal
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Favicon
A RAG for Elixir in Elixir
Favicon
Enhancing Elixir Development in LazyVim: Quick Documentation Access by Telescope
Favicon
Learning Elixir: Control Flow with If and Unless
Favicon
Pseudolocalization in Phoenix with gettext_pseudolocalize
Favicon
The Journey of Optimization
Favicon
How to use queue data structure in programming
Favicon
Phoenix LiveView, hooks and push_event: json_view
Favicon
🥚 Crack Open These 20+ Elixir Goodies
Favicon
Learning Elixir: Understanding Atoms, Booleans and nil
Favicon
Unlocking the Power of Elixir Phoenix and Rust: A Match Made for High-Performance Web Applications
Favicon
Elixir: Concurrency & Fault-Tolerance
Favicon
Enhancements to dbg in elixir 1.18
Favicon
Learning Elixir: Working with Strings
Favicon
Leverage ETS for Shared State in Phoenix
Favicon
Elixir em Foco em 2024
Favicon
Building HTTP/JSON API In Gleam: Introduction
Favicon
Phoenix
Favicon
Sql commenter with postgrex
Favicon
Learning Elixir: Understanding Numbers
Favicon
For loops and comprehensions in Elixir - transforming imperative code
Favicon
Phoenix LiveView is slot empty?
Favicon
Customizing IEx: Personalizing Your Elixir Shell Environment
Favicon
Masters of Elixir: A Comprehensive Collection of Learning Resources
Favicon
Leveraging GenServer and Queueing Techniques: Handling API Rate Limits to AI Inference services
Favicon
Chekhov's gun principle for testing
Favicon
Solving Advent Of Code 2024 on a elixir project
Favicon
Bridging the Gap: Simplifying Live Component Invocation in Phoenix LiveView
Favicon
Harness PubSub for Real-Time Features in Phoenix Framework
Favicon
Debugging with dbg: Exploring Elixir's Built-in Debugger
Favicon
New to dev.to and Excited to Share ProxyConf: My Elixir-Powered API Control Plane

Featured ones: