lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Replying to myself

> Am 23.09.2021 um 09:39 schrieb Marc Balmer <marc@msys.ch>:
> 
> 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.
> 

Instead of

function stat ()
  local res <const>

  if cond == 1 then
    res = db:exec('select * from whatever')
  else
    res = db:exec('select * from somethingelse')
  end

  -- do something with res
end



I can use a nested function:

function stat ()

  local function doSomething(res)
    -- do something with res
  end

  if cond == 1 then
    local res <close> = db:exec('select * from whatever')
    doSomething(res)
  else
    local res <close> = db:exec('select * from somethingelse')
    doSomething(res)
  end
end