Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

Call a function by reference


Question

Posted (edited)

I was wondering,,
can we just calll a function by referencing his name??

 

Please login or register to see this code.

or this is totally nonesense??

Edited by Neo Andersson

8 answers to this question

Recommended Posts

  • 0
Posted

You can do the following

 

 

 
function myfun1(args);
end;
 
function myfun2(args);
end;
 
callTheseOnChange = {{name = function() myfun1(args) end , name = function() myfun2(args) end}}
 
--and somewhere in codes just call
for key, f in pairs(callTheseOnChange) do
  f.name()
end

 
  • 0
Posted (edited)

It's not a nonsense. In LUA function is just value 
You may do the following:

Please login or register to see this code.

As you see, function is in a table, call is indexed by table element name equal to function name...

 

As far as I know, even in statically typed languages like C++, you can call a function through a variable that is a pointer to it.

Edited by Łukasz997
  • Thanks 1
  • 0
Posted

Please login or register to see this code.

Yes, so global we can get via _G, but local functions we need to put in a table as we are a bit restricted in Fibaro's Lua environment.

...or put them directly in the callTheseOnChange table if they are small and dedicated for just that task...

  • Thanks 1
  • 0
  • Inquirer
  • Posted
    44 minutes ago, jgab said:

    Please login or register to see this code.

    Yes, so global we can get via _G, but local functions we need to put in a table as we are a bit restricted in Fibaro's Lua environment.

    ...or put them directly in the callTheseOnChange table if they are small and dedicated for just that task...

     

     

    @jgab jan this doesnt work. Throws error  complaining about fun is nil..
    Can we call it like funTable[fun]() even if the keys are not strings?? Isnt the problem coming from that??

    • 0
  • Inquirer
  • Posted

    Guys thanks for the helps.
    My goal was to list name of functions (strings) in a table,  (not define them there) just list them, so later
    i can loop through that table and call the function that names are listed there..

    • 0
    Posted
    18 minutes ago, Neo Andersson said:

    Guys thanks for the helps.
    My goal was to list name of functions (strings) in a table,  (not define them there) just list them, so later
    i can loop through that table and call the function that names are listed there..

    Myfun1 in table should be MyFun1 etc...

    • 0
    Posted

    Here you are exactly as you want, a mini API :). For sure it's working. By the way, how do you want to use it?
     

    Please login or register to see this code.

     

    • 0
  • Inquirer
  • Posted
    2 minutes ago, Łukasz997 said:

    Here you are exactly as you want, a mini API :). For sure it's working. By the way, how do you want to use it?
     

    Please login or register to see this code.

     

    Thanks..
    usage is, i want to define some http calls that will plpulate some variable..Depending on what variable is populated, the code should fetch the variable name in _G table, and find the corresponding function (in our table that u showed) to be run, when that _G is set.

    It can be done ofcoure easiliy in the http success callback, and hardcode it there what should be called, but if you have many many of these ocasions it makes my life easier, to predefine, what function should be called in case of some _G variable change

    and ofcoure the reason is also to learn some new stuff :-)

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...