Search code examples
luaneovim

Lua multiple assignment with and without variable declaration


Is there a way to accomplish the following:

local status, foo = pcall(require, "foo")
    ...
local status, bar = pcall(require, "bar") <-error here: Redefined local 'status'

In the second line, is there a way to describe only bar as being a new local declaration?


Solution

  • Use 2 steps to re-use status variable:

    local status, foo = pcall(require, "foo")
    (...)
    local bar
    status, bar = pcall(require, "bar")