Trying to figure out what a [] [] does in a macro assignment

noob here. Trying to understand some code and am baffled as to how to figure out the utility of the TWO in the below.

@use_memo(deps::Vector{Any}) do
# Expensive computation/loading
end

messages = @use_memo(() -> [], []   ) # THIS IS THE CODE THAT CONFUSES ME

Running it in the repl I see it returns a vector of any BUT I don’t understand what’s going on? what is an alias for Array and why is it useful?

using PlutoHooks

julia> messages = @use_memo(() -> [], [])
Any[]

julia> typeof( messages)
Vector{Any} (alias for Array{Any, 1})

You can use @macroexpand to see what the macro expands to.

That’s just the way type aliases are printed in the REPL.

1 Like

Hi there
thanks for the reply @kristoffer.carlsson BUT I tried that and got

ulia> using PlutoHooks

julia> messages = @use_memo(() -> [], []   )
Any[]

julia> @macroexpand( @use_memo() )
ERROR: MethodError: no method matching var"@use_memo"(::LineNumberNode, ::Module)
Closest candidates are:
  var"@use_memo"(::LineNumberNode, ::Module, ::Any, ::Any) at ~/.julia/packages/PlutoHooks/udh31/src/notebook.jl:56
Stacktrace:
 [1] #macroexpand#61
   @ ./expr.jl:115 [inlined]
 [2] top-level scope
   @ REPL[3]:1

julia> @macroexpand( @use_memo )
ERROR: MethodError: no method matching var"@use_memo"(::LineNumberNode, ::Module)
Closest candidates are:
  var"@use_memo"(::LineNumberNode, ::Module, ::Any, ::Any) at ~/.julia/packages/PlutoHooks/udh31/src/notebook.jl:56
Stacktrace:
 [1] #macroexpand#61
   @ ./expr.jl:115 [inlined]
 [2] top-level scope
   @ REPL[4]:1

julia> 

what am I missing please?

The macro call @use_memo(() -> [], [] ) is working, but your trying to expand the @use_memo() which is not working as the macro requires two arguments. Just try

@macroexpand @use_memo(() -> [], []   )
1 Like

thanks @bertschi

one step forward several thousand back :slight_smile:

serves me right for :

  • trying to dodge metaprogramming
  • lifting the @macroexpand rock

thanks for taking the time both @bertschi and @kristoffer.carlsson

 @macroexpand @use_memo(() -> [], []   )
quote
    #= /home/dave/.julia/packages/PlutoHooks/udh31/src/notebook.jl:58 =#
    var"#1#ref" = begin
            #= /home/dave/.julia/packages/PlutoHooks/udh31/src/notebook.jl:112 =#
            var"#1#ref" = Base.RefValue{Any}(#undef)
            #= /home/dave/.julia/packages/PlutoHooks/udh31/src/notebook.jl:113 =#
            var"#1#ref"[] = PlutoHooks.nothing
            #= /home/dave/.julia/packages/PlutoHooks/udh31/src/notebook.jl:114 =#
            var"#1#ref"
        end
    #= /home/dave/.julia/packages/PlutoHooks/udh31/src/notebook.jl:59 =#
    if #= /home/dave/.julia/packages/PlutoHooks/udh31/src/notebook.jl:334 =#, [], #= /home/dave/.julia/packages/PlutoHooks/udh31/src/notebook.jl:335 =#, true
        #= /home/dave/.julia/packages/PlutoHooks/udh31/src/notebook.jl:60 =#
        var"#1#ref"[] = ((()->begin
                    #= REPL[2]:1 =#
                    []
                end))()
    end
    #= /home/dave/.julia/packages/PlutoHooks/udh31/src/notebook.jl:62 =#
    var"#1#ref"[]
end

julia>