gcalderone/Gnuplot.jl

when pass session name as a variable, it breaks (as of v1.6.something)

ahbarnett opened this issue · 6 comments

Not exactly sure when this broke, but I used to rely on it in v1.5 something:

@gp :fig1 rand(10) "w l"                   # works fine
s = :fig1
@gp s rand(10) "w l"                   # breaks
@gp session_names()[1] rand(10) "w l"     # also breaks.

The error is:

ERROR: Unexpected argument with type Symbol
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] parseSpecs(::Symbol, ::Vararg{Any}; default_mid::Int64, is3d::Bool, kws::@Kwargs{})
   @ Gnuplot ~/.julia/packages/Gnuplot/PdysS/src/plotspecs.jl:200
 [3] top-level scope
   @ REPL[17]:1

Thanks for a useful package! Best, Alex

It broke in v1.6. The problem is that the session name needs to be a literal Symbol, while variables of type Symbol are no longer accepted. This is why @gp :fig1 ... works while @gp s ... raises an error.
The advantage is that a @gp invocation now involves exactly one session.
Sorry for this breaking change, but I had to rationalize a bit the syntax...

If you still wish to use a Symbol in a variable you can copy/paste the code generated by the @gp macro, e.g.:

@macroexpand @gp :fig1 rand(10) "w l"
quote
    local gp = Gnuplot.getsession(:fig1)
    Gnuplot.reset(gp)
    Gnuplot.append!(gp, Gnuplot.parseSpecs(rand(10), "w l", default_mid = Gnuplot.last_added_mid(gp), is3d = false))
    Gnuplot.options.gpviewer && gpexec.(Ref(gp), Gnuplot.collect_commands(gp))
    gp
end

and replace :fig1 with a variable containing a session name such as s from your example, or session_names()[1]

I don't know metaprogramming, but I don't see why you couldn't allow a Symbol as well as a QuoteNode in the logic here:

if (first <= length(args)) && isa(args[first], QuoteNode)

That would recover the rather useful behavior from v1.5. Otherwise unpacking the macro is awkward.

Here's me checking the symbol or quotenode

julia> macro t(args...)
       println(args[1]," ",isa(args[1],Symbol)," ",isa(args[1],QuoteNode))
       end
@t (macro with 1 method)

julia> @t :fig1
:fig1 false true

julia> s= :fig1; @t s
s true false

Since the command Gnuplot.getsession(s) works on symbols as well as quotenodes, this should be a 1-line change (the line above).
Thanks, Alex

Because isa(args[1],Symbol) is a check on the name of the variable, not its content.
If you set s to an int or a string you would obtain exactly the same results:

s= :fig1; @t s
s true false

julia> s= 1; @t s
s true false

julia> s= "ddd"; @t s
s true false

An alternative approach is to change the name of the default session being addressed when no one is specified, e.g.:

s = :fig1; Gnuplot.options.default = s; @gp rand(10) "w l"

I'm working on new examples, and I will also add this.
Thank you for pointing it out!