# Is there a built in macro like @show that returns a string?

**URL:** <https://discourse.julialang.org/t/is-there-a-built-in-macro-like-show-that-returns-a-string/88234>\
**Category:** New to Julia\
**Tags:** macros, strings, show\
**Created:** [October 4, 2022, 2:34pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-macro-like-show-that-returns-a-string/88234 "2022-10-04T14:34:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![taotree](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taotree/32/31982_2.png) [@taotree](https://discourse.julialang.org/u/taotree)\
**Post date:** [October 4, 2022, 2:34pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-macro-like-show-that-returns-a-string/88234/1 "2022-10-04T14:34:09Z")

</div>

I want the macro @show but to return a string instead of printing. Does something like that already exist?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 4, 2022, 2:49pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-macro-like-show-that-returns-a-string/88234/2 "2022-10-04T14:49:41Z")

</div>

Not that I know of, but it would be pretty easy to write this by copying [the `@show` definition](https://github.com/JuliaLang/julia/blob/c609e9a22a17bef858559ca707cb985d770935aa/base/show.jl#L1124-L1132). e.g.

```julia
macro showstring(exs...)
    args = []
    for ex in exs
        push!(args, sprint(Base.show_unquoted, ex), " = ", esc(ex), '\n')
    end
    return :(string($(args...)))
end

```

Why do you need such a macro? Seems a little odd to me.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [October 4, 2022, 3:07pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-macro-like-show-that-returns-a-string/88234/3 "2022-10-04T15:07:57Z")

</div>

You might be interested in the `repr` function. It doesn’t have the pretty `x = ...` part but that’s easy to do manually and not a difficult macro otherwise.

```julia
julia> "x = " * repr(rand(1:10,2,3))
"x = [9 6 3; 8 4 5]"

```

---

<div class="post-metadata">

**Author:** ![taotree](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taotree/32/31982_2.png) [@taotree](https://discourse.julialang.org/u/taotree)\
**Post date:** [October 4, 2022, 3:28pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-macro-like-show-that-returns-a-string/88234/4 "2022-10-04T15:28:47Z")

</div>

Thanks. Well, among other things, I thought it might be handy when using @assert. The second argument in @assert isn’t very flexible: it’s just a string, but I’d really like it to automatically show everything in the expression. I didn’t want to replace @assert because in the docs it says: “An assert might be disabled at various optimization levels.” which is a good thing. I suppose I could implement my own mechanism to deactivate my @assert replacement. Actually, that might be ideal.

But, I regularly find myself a bit frustrated with functions that print instead of returning a string… So, maybe what I need is a macro that converts a function that takes an IO as first arg to return a string.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 4, 2022, 3:34pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-macro-like-show-that-returns-a-string/88234/5 "2022-10-04T15:34:38Z")

</div>

> [@taotree](#):
>
> So, maybe what I need is a macro that converts a function that takes an IO as first arg to return a string.

You [don’t need a macro for this](https://www.youtube.com/watch?v=mSgXWpvQEHE&list=PLP8iPy9hna6StY9tIJIUN3F_co9A0zh0H&t=575s). That’s exactly what the [`sprint` function](https://docs.julialang.org/en/v1/base/io-network/#Base.sprint) does. Or, at a lower level, you can [print to an `IOBuffer`](https://docs.julialang.org/en/v1/base/io-network/#Base.IOBuffer) and then `take!` that to a string — this is how `sprint` is implemented.

---

<div class="post-metadata">

**Author:** ![taotree](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taotree/32/31982_2.png) [@taotree](https://discourse.julialang.org/u/taotree)\
**Post date:** [October 4, 2022, 4:07pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-macro-like-show-that-returns-a-string/88234/6 "2022-10-04T16:07:22Z")

</div>

> [@stevengj](#):
>
> That’s exactly what the [`sprint` function](https://docs.julialang.org/en/v1/base/io-network/#Base.sprint) does.

Thanks! I thought I had just run across something that does that but I couldn’t find it again.

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [October 13, 2024, 12:39pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-macro-like-show-that-returns-a-string/88234/7 "2024-10-13T12:39:04Z")

</div>

> [@stevengj](#):
>
> Why do you need such a macro? Seems a little odd to me.

I’m not the OP and this thread is old, but I’ve come here because I want to replace `@show . . . ` with `@info . . . maxlog=1`:

```julia
function call_me_many_times()
   # . . . 
   @show array1, array2
end

```

`@show` is more convenient because it shows the names of the variables and its contents in a more compact way than `@info` but the above use of `@show` can be annoying when the function is called many times while you just want to check the working of your function once. On the other hand, `@info` has a nice flag `maxlog=1`, so I would use your macro like this:

```julia
using Logging
function call_me_many_times()
   # . . .
   s = @showstring(array1, array2)
   @info s maxlog=1
end

```

It’s a bit strange to me that no means is provided to specify which I/O stream that @show will send the string into. (Compare with the family `printf`, `sprintf`, and `fprintf` in C.)

[Before you point out, I’ve read the thread where the OP asks how to execute only once a section of code within a function. The solutions there are to me a bit overkill just to avoid multiple `@show`’s.]

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [October 13, 2024, 3:21pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-macro-like-show-that-returns-a-string/88234/8 "2024-10-13T15:21:10Z")

</div>

Doesn’t `@info "" array1 array2` work for you? It supports all `@info` options and prints variable names as well.

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [October 13, 2024, 4:01pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-macro-like-show-that-returns-a-string/88234/9 "2024-10-13T16:01:28Z")

</div>

> [@aplavin](#):
>
> Doesn’t `@info "" array1 array2` work for you? It supports all `@info` options and prints variable names as well.

Yes and yes. I should’ve highlighted that, in my previous message. Here I quote my words:

> [@](#):
>
> `@show` is more convenient because it shows . . . its contents in a _ **more compact way** _ than `@info` . . .

[Emphasis is added here.] `@info` is verbose:

```julia
julia> a = [1,2,3,4]

julia> @show a;
a = [1, 2, 3, 4]

julia> using Logging

julia> @info "" a;
┌ Info:
│ a =
│ 4-element Vector{Int64}:
│ 1
│ 2
│ 3
└ 4

```

So, if there is an option to `@info` to use a `@show` like compact format, then that would be what I’m looking for. (I suppose that with the `Logging` package, the destinations of the logs can also be controlled, right?)

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [October 13, 2024, 11:41pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-macro-like-show-that-returns-a-string/88234/10 "2024-10-13T23:41:40Z")

</div>

That’s just the display of the default ConsoleLogger; you can fully customize logging and set your logger to display the information however you like (with some work), and to send the info wherever you like (to the repl, to a file, over the network, etc). See the JuliaLogging website for an overview: [https://julialogging.github.io/](https://julialogging.github.io/)
