Can someone explain when to use a local function and when a 'normal/non-local' function ?
And maybe you are the right person also who can explain a local variable and how to use a global variable ?
Difference local function and function
Moderator: leecollings
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: Difference local function and function
There are books written about this question and these are freely available on the internet. If you don't have time to study these, the rule of thumb is to use local everywhere for functions and variables where possible. It limits the scope of them (less hard to find bugs) and in Lua it has also significant performance advantages. ref
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
- Posts: 4
- Joined: Sunday 17 February 2019 19:08
- Target OS: NAS (Synology & others)
- Domoticz version:
- Contact:
Re: Difference local function and function
I google'd a lot on this subject, didn't find one LUA example in combination with Domoticz which explains the difference.waaren wrote: ↑Wednesday 10 June 2020 20:42There are books written about this question and these are freely available on the internet. If you don't have time to study these, the rule of thumb is to use local everywhere for functions and variables where possible. It limits the scope of them (less hard to find bugs) and in Lua it has also significant performance advantages. ref
Someone has a better idea or a link to an example ?
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: Difference local function and function
The use of local in Lua is not domoticz related. It's valid for Lua as a programming language. If embedded in an application like domoticz or used as a stand alone language.
Did you actually clicked the link in my post ?
ref
Also this post.
this post
And this example
Code: Select all
local function outer()
function inner() -- This function can be used in this script below this line
return('inner')
end
return 'outer-' .. inner()
end
print('Calling outer() ==>> ' .. outer() ) -->> prints: Calling outer() ==>> outer-inner
print ('Calling inner() ==>> ' .. tostring(inner() )) -->> prints: Calling inner() ==>> inner
local function outerL()
local function innerL() -- This function can only be used within the scope of outerL
return('innerL')
end
return 'outerL-' .. innerL()
end
print('Calling outerL() ==>> ' .. outerL() ) -->> prints: Calling outerL() ==>> outerL-innerL
print ('Calling innerL() ==>> ' .. tostring(innerL() )) -->> error: attempt to call a nil value (global 'innerL')
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
- Posts: 4
- Joined: Sunday 17 February 2019 19:08
- Target OS: NAS (Synology & others)
- Domoticz version:
- Contact:
Re: Difference local function and function
Thnx, i am a newbie but i am getting it bit for bit. Now the question remains: where do i declare my own global variables in Domoticz-LUA. I know Lua nows time, device, etc. events. So where to put them ?
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: Difference local function and function
Basically; you don't.
They will be lost after your script finished. If you need to keep values between script executions in classic Lua, you need to store them in domoticz uservariables or as an attribute of a device.
in dzVents you can do as in classic Lua and/or use persistent data to store values. Persistent data is stored in OS files between script executions.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Who is online
Users browsing this forum: No registered users and 1 guest