Strange results with slurping

Here is a simple example that demonstrates that the ... list expansion operator can exhibit strange behavior. Consider:

function tst(args...)
    @show args
end

arg1 = (a=3, b=4)
arg2 = (c=5, d=7)
arg3 = (c=5, f=7)

tst(arg1...);
tst(arg1..., arg2...);    # <<< CASE 2
tst(arg1..., arg2..., arg3...);

which prints the result:

args = (3, 4)

3457

args = (3, 4, 5, 7, 5, 7)

The question is: why does the second example produce an output format different than the other two?
Thanks for any insight.

1 Like

what version of Julia are you on?

1 Like

Version 1.8.3
Hard to imagine this kind of error occurring, even on less mature versions of Julia.

I don’t think it does. I get exactly what you’d expect in Julia 1.8:

julia> tst(arg1...);
args = (3, 4)

julia> tst(arg1..., arg2...);    # <<< CASE 2
args = (3, 4, 5, 7)

julia> tst(arg1..., arg2..., arg3...);
args = (3, 4, 5, 7, 5, 7)

My guess is that you have some other method tst in your current session which happens to be more specific and applicable for the 4-argument case.

5 Likes

Is there any good way to remove a method from the methodtable? I usually restart my session, but that can be inconvenient sometimes.

1 Like

Base.delete_method

4 Likes

Could be nice?

julia> tst(arg1...);
args = (3, 4)

julia> macro delete_which(ex)
           print("Warning: removing methods may cause unexpected behavior!\n\nDo you wish to proceed? [y/n]: ")
           yn=readline(stdin)
           if yn ≠ "y"; println("Method deletion cancelled")
           else println("Deleting @which ", ex); esc(:( Base.delete_method(@which $ex) )) end
       end
@delete_which (macro with 1 method)

julia> @delete_which tst(arg1...)
Warning: removing methods may cause unexpected behavior!

Do you wish to proceed? [y/n]: y
Deleting @which tst(arg1...)

julia> tst(arg1...);
ERROR: MethodError: no method matching tst(::Int64, ::Int64)