# Is there a \`@print\` macro?

**URL:** <https://discourse.julialang.org/t/is-there-a-print-macro/47897>\
**Category:** New to Julia\
**Created:** [October 6, 2020, 10:30pm UTC](https://discourse.julialang.org/t/is-there-a-print-macro/47897 "2020-10-06T22:30:03Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 6, 2020, 10:30pm UTC](https://discourse.julialang.org/t/is-there-a-print-macro/47897/1 "2020-10-06T22:30:03Z")

</div>

I am looking for a macro just like `@show` but is less cluttered. The behavior is

```julia
@print x = 100

# same as 

x = 100
println(x)
x

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 7, 2020, 12:18am UTC](https://discourse.julialang.org/t/is-there-a-print-macro/47897/2 "2020-10-07T00:18:54Z")

</div>

Like this?

```julia
julia> macro print(x)
         println(x)
         x
       end
@print (macro with 1 method)

julia> @print x = 100
x = 100
100

julia> @print x = rand(2)
x = rand(2)
2-element Array{Float64,1}:
 0.6774432057224766
 0.6444360280880168

julia>

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 7, 2020, 8:22am UTC](https://discourse.julialang.org/t/is-there-a-print-macro/47897/3 "2020-10-07T08:22:50Z")

</div>

Why would you need a macro for this?

```julia
function also_print(x)
    println(x)
    x
end

```

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [October 7, 2020, 11:08am UTC](https://discourse.julialang.org/t/is-there-a-print-macro/47897/4 "2020-10-07T11:08:17Z")

</div>

Have you compared the output of `also_print` with `@print` from previous post?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 7, 2020, 11:10am UTC](https://discourse.julialang.org/t/is-there-a-print-macro/47897/5 "2020-10-07T11:10:05Z")

</div>

I was answering the original question. I don’t think that in its current form needs a macro.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 7, 2020, 12:37pm UTC](https://discourse.julialang.org/t/is-there-a-print-macro/47897/6 "2020-10-07T12:37:28Z")

</div>

Not everything needs a macro!

When I’m working on problem sets I like doing a pseudo-literate programming where I just print a bunch of stuff. A `@print` macro here saves typing thats all.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [October 7, 2020, 1:36pm UTC](https://discourse.julialang.org/t/is-there-a-print-macro/47897/7 "2020-10-07T13:36:53Z")

</div>

I think the OP wants a function that does not print `x = 100` but only `100`, as `@show` already does this and the OP wants something ‘less cluttered’.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 7, 2020, 1:41pm UTC](https://discourse.julialang.org/t/is-there-a-print-macro/47897/8 "2020-10-07T13:41:42Z")

</div>

Not sure what he wants :-), `@show` is more clutered still:

```julia
julia> @show x = 100
x = 100 = 100
100

```

Which is particularly bad for large vectors:

```julia
julia> @show x = rand(100)
x = rand(100) = [0.7932685917277631, 0.171562838430672, 0.1533053287615691, 0.5940641659944244, 0.7894845386560969, 0.48648370583714073, 0.884517876814745, 0.6735656113119755, 0.9021839594760872, 0.03670723433507406, 0.27672480054279425, 0.3792029967660018, 0.6008286443369579, 0.15603263342637197, 0.2634425479242821, 0.3185735573879902, 0.40028718656287987, 0.20382145243272132, 0.6001099496665276, 0.821283170134512, 0.011429640846802203, 0.011673396918208345, 0.20431121205244285, 0.4472993063139954, 0.25787869477341907, 0.000845508614282009, 0.17617054670506738, 0.9310098624594569, 0.6904302114484664, 0.34568527555500883, 0.5540844341273561, 0.9541833736733725, 0.7784457185663476, 0.6438840182392767, 0.675839775989125, 0.3100780261515006, 0.8709115784917425, 0.8918887890659433, 0.35010109019279256, 0.40777265901479565, 0.394307318592402, 0.19305728133947464, 0.8248792162585294, 0.03691464460671723, 0.861178559944763, 0.12135771159419995, 0.014598244734525201, 0.5420625944454087, 0.4134958941665783, 0.10964818331129389, 0.5398530525234055, 0.6294666940633684, 0.5816820532325195, 0.3547054253374502, 0.7583747553488567, 0.2588930997140726, 0.1464479495203268, 0.10134508442630841, 0.5500317809128956, 0.29284519015214605, 0.227207469353494, 0.4436538058962731, 0.8334815642720053, 0.2670543321170826, 0.8768770993079196, 0.4857084418273774, 0.8743972998335523, 0.486432528628314, 0.11219676895086739, 0.4340638733724731, 0.4801410742631953, 0.2381232989059181, 0.7815242908509641, 0.9265332299614948, 0.42156231981023695, 0.2492129241810921, 0.8891203728530099, 0.6966144038711259, 0.4999874026091602, 0.10573687757916961, 0.6189071621092643, 0.46713003351297755, 0.07581711983217954, 0.8580270733374453, 0.3275419760835274, 0.09801399209953643, 0.05107540238866637, 0.5801894455847458, 0.7523427736721597, 0.8140274541957271, 0.1409097471600147, 0.6929317731842422, 0.8375466539193652, 0.29711695810908, 0.5800128353423308, 0.8352277151011072, 0.8607085163044783, 0.10932125511939339, 0.5759679277557357, 0.14908679117876344]
100-element Array{Float64,1}:
 0.7932685917277631
 0.171562838430672
 0.1533053287615691
 0.5940641659944244
 0.7894845386560969
 0.48648370583714073
 0.884517876814745
 0.6735656113119755
 0.9021839594760872
 0.03670723433507406

```

Anyway, any of the options is easy to obtain.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [October 7, 2020, 1:53pm UTC](https://discourse.julialang.org/t/is-there-a-print-macro/47897/9 "2020-10-07T13:53:08Z")

</div>

It has to be a macro if they want to put the binding inside the call, otherwise no binding will happen and Julia will scream that the method has not a keyword argument with name `x`.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 7, 2020, 1:54pm UTC](https://discourse.julialang.org/t/is-there-a-print-macro/47897/10 "2020-10-07T13:54:03Z")

</div>

I think this is what I want

```julia

julia> macro print(x)
       t = gensym()
       esc(quote
           $t = $x
           println($t)
           $t
       end)
       end

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 7, 2020, 2:03pm UTC](https://discourse.julialang.org/t/is-there-a-print-macro/47897/11 "2020-10-07T14:03:43Z")

</div>

Since you are not using `x` as an expression, only its value, I still think you can do this with a function, eg the one above. Then `y = @print(expr)` is equivalent to `y = also_print(expr)`.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 7, 2020, 2:06pm UTC](https://discourse.julialang.org/t/is-there-a-print-macro/47897/12 "2020-10-07T14:06:51Z")

</div>

This is true. But sometimes you want to save on typing, not worry about parentheses etc.

---

<div class="post-metadata">

**Author:** ![roble](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roble/32/12303_2.png) [@roble](https://discourse.julialang.org/u/roble)\
**Post date:** [October 7, 2020, 2:29pm UTC](https://discourse.julialang.org/t/is-there-a-print-macro/47897/13 "2020-10-07T14:29:05Z")

</div>

What about the `@printf` macro? It also gives you the option to format your output in a c style manner

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 7, 2020, 3:45pm UTC](https://discourse.julialang.org/t/is-there-a-print-macro/47897/14 "2020-10-07T15:45:10Z")

</div>

`@printf` can’t work with more complicated expressions because it looks for all sorts of `%s` flags inside the expression.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [October 7, 2020, 6:16pm UTC](https://discourse.julialang.org/t/is-there-a-print-macro/47897/15 "2020-10-07T18:16:10Z")

</div>

What exactly creating symbol `t` was supposed to achieve there?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 7, 2020, 6:19pm UTC](https://discourse.julialang.org/t/is-there-a-print-macro/47897/16 "2020-10-07T18:19:50Z")

</div>

You don’t want to evaluate the expression more than once. See the following

```julia
julia> macro print(x)
       esc(quote
           println($x)
           $x
           end
       )
       end
@print (macro with 1 method)

julia> function hithere()
       println("Hi there!")
       return 0
       end
hithere (generic function with 1 method)

julia> @print hithere()
Hi there!
0
Hi there!
0 # this last one is just cause we are at the REPL

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [October 7, 2020, 6:21pm UTC](https://discourse.julialang.org/t/is-there-a-print-macro/47897/17 "2020-10-07T18:21:14Z")

</div>

Ah, this is true. I have forgot about side-effects after Tamas pointed out you were using only the value of the expression.
