I am looking for a macro just like @show
but is less cluttered. The behavior is
@print x = 100
# same as
x = 100
println(x)
x
I am looking for a macro just like @show
but is less cluttered. The behavior is
@print x = 100
# same as
x = 100
println(x)
x
Like this?
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>
Why would you need a macro for this?
function also_print(x)
println(x)
x
end
Have you compared the output of also_print
with @print
from previous post?
I was answering the original question. I don’t think that in its current form needs a macro.
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.
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’.
Not sure what he wants :-), @show
is more clutered still:
julia> @show x = 100
x = 100 = 100
100
Which is particularly bad for large vectors:
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.
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
.
I think this is what I want
julia> macro print(x)
t = gensym()
esc(quote
$t = $x
println($t)
$t
end)
end
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)
.
This is true. But sometimes you want to save on typing, not worry about parentheses etc.
What about the @printf
macro? It also gives you the option to format your output in a c style manner
@printf
can’t work with more complicated expressions because it looks for all sorts of %s
flags inside the expression.
What exactly creating symbol t
was supposed to achieve there?
You don’t want to evaluate the expression more than once. See the following
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
Ah, this is true. I have forgot about side-effects after Tamas pointed out you were using only the value of the expression.