Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Call macro dynamically — Gideros Forum

Call macro dynamically

saeyssaeys Member
edited September 2017 in General questions
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()
+1 -1 (+1 / -0 )Share on Facebook

Comments

  • HubertRonaldHubertRonald Member
    edited September 2017
    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 Maintainer
    Accepted Answer
    @saeys, you just can't!
    Macros only exists at compile time, no longer at run time.

    Likes: antix, saeys

    +1 -1 (+2 / -0 )Share on Facebook
  • :(( @hgy29 That's a bit beyond my comprehension, but fair enough, accepting it and moving on... Thanks for quick answers! :)
Sign In or Register to comment.