Quick Links: Gideros Home | Download Gideros | Developer Guide
Call macro dynamically
  • Is there a way to call a macro from a string representing its name? Like one can do with function names:
    newfunction = _G[someFunctionName]
    newfunction()
  • I don't know if you need this exactly
    hiThere = function (msg) return tostring(msg) end
     
    local g='hiThere'
    print(assert(loadstring('return '..g..'(...)'))("Hello World!"))
     
    -- print: Hello World!

    @saeys you can see this link with more information about it
    https://stackoverflow.com/questions/1791234/lua-call-function-from-a-string-with-function-name
  • Sorry for being unclear... :\">

    I'm already doing the
    x='foo'
    _G[x]() -- calls foo from the global namespace
    example described in that stackoverflow link, but I would like to use macros instead of functions, for the sake of memory (and speed?). But I'm not sure if that's possible, or if possible how to call those macros dynamically.

    To be extra clear, right now I'm doing something like this and it's working fine:
    function snippetA()
    --some code
    end
     
    --To call the above function
    letter = "A"
    whatToCall = "snippet" .. letter
    call = _G[whatToCall]
    call() --> "some code" get executed

    But I'd like to use macros instead:
    snippetA @ |
    --some code
    |
     
    -- How can I call the above macro in the same manner as the function?
    letter = "A"
    whatToCall = "snippet" .. letter
    -- now what? The _G[whatToCall] obviously doesn't work.
  • hgy29hgy29 +1 -1 (+2 / -0 )
    Maintainer Accepted Answer
    @saeys, you just can't!
    Macros only exists at compile time, no longer at run time.

    Likes: antix, saeys

  • :(( @hgy29 That's a bit beyond my comprehension, but fair enough, accepting it and moving on... Thanks for quick answers! :)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Login with Facebook Sign In with OpenID

In this Discussion

Top Posters