smetana

smetana

Hello all,

I need to convert AST with possible long binary to string.
Macro.to_string() uses Inspect and Inspect has the printable_limit option set to 4096 for binaries by default.
I am looking for the way to increase it so binary will not be truncated on to_string()

Thanks,
Serge

Showing Posts 1 to 10

michalmuskala

michalmuskala

This seems like a bug in Macro.to_string to me - it should print everything out. Could you open a bug report?

net

net

I believe this is already fixed in master. Until that’s released you can do

Macro.to_string(v, fn
  node, _ when is_binary(node) -> inspect(node, printable_limit: :infinity)
  _, string -> string
end)
mruoss

mruoss

Now that Macro.to_string/2 is deprecated, how would one do this?

axelson

axelson

Scenic Core Team

The bug is fixed so now you can use Macro.to_string/1 and if you’re trying to do something else then that would be best to discuss in a new thread where you state what you’re trying to accomplish.

mruoss

mruoss

This is too related to open a new thread. But when posting before, I wasn’t aware that the problem exists only for maps now. It’s basically the same issue but for maps. Opened a GH issue here:

https://github.com/elixir-lang/elixir/issues/13875

axelson

axelson

Scenic Core Team

Ah, I see. Yeah that does look like a related bug to me :+1:

mruoss

mruoss

Copying over José’s answer for shortcuts:

The difference here is that maps are not valid AST. We fallback to inspect in Macro.to_string for convenience, because the goal of the macro is mostly debugging, but if you returned that from a macro, Elixir would raise accordingly. In any case, it won’t be formatted accordingly. The correct fix is to call Macro.escapeor build the correct Map ast, as shown in the result of quote do: %{1 => 2, foo: :bar}|

For me, Macro.escape did the trick:

my_map = Map.new(1..51, &{&1, "value"}) |> Macro.escape()

(quote do: unquote(my_map))
|> Macro.to_string()
|> IO.inspect(printable_limit: :infinity)
nhpip

nhpip

Ironically I had a similar issue the other day. Code.quoted_to_algebra/2 offers a lot more control:

macro_to_string = 
    fn m ->
       Code.quoted_to_algebra(m,
           pretty: true,
           locals_without_parens: [field: 4, field: 3, field: 2, value: 2, value: 3]
       )
       |> Inspect.Algebra.format(:infinity)
       |> IO.iodata_to_binary()
  end
mruoss

mruoss

This doesn’t truncate the map. However, it still does a better job formatting the code when escaping the map to a valid AST.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews