Why not:
function stat()
local function doSomething(query)
local res <const> = db:exec(query)
-- do something with res
end
if cond == 1 then
doSomething( 'select * from whatever')
else
doSomething('select * from somethingelse')
end
end
-- here the res can be <const> as you want and as well <toclose> for being closed at end of doSomething())
Or:
function stat()
local function makeQuery()
if cond == 1 then
query = 'select * from whatever'
else
query = 'select * from somethingelse'
end
end
local function doSomething(res)
-- do something with res
end
local qry <const> = makeQuery()
local res <const> = db:exec(qry)
end
-- here the res and query can both be <const>, and as well both declared <toclose> for being closed at end of stat()
Or a bit more compactly:
function stat()
local function doSomething(res)
-- do something with res
end
local qry <const> = (function()
if cond == 1 then
return 'select * from whatever'
else
return 'select * from somethingelse'
end
end)()
local res <const> = db:exec(qry)
end
Or very compactly:
function stat()
(function(res)
-- do something with res
end)(db:query((function()
if cond == 1 then
return 'select * from whatever'
else
return 'select * from somethingelse'
end
end)()))
end
Le jeu. 23 sept. 2021 à 09:48, Marc Balmer <
marc@msys.ch> a écrit :
> Is it somehow possible to make one time assignment to a to-be-closed variable? E.g. something like this:
>
> local res <const>
>
> if cond == 1 then
> res = db:exec('select * from whatever')
> else
> res = db:exec('select * from somethingelse')
> end
>
> Or is it so that to-be-closed variables can only be assigned a value during creation?
>
> If the latter: It would be useful to allow for one time assignment to to-be-closed variabled.