Can I get a multiple instances of a library?

Lua caches results from require in package.loaded:

local lib1 = require "some_library"
local lib2 = require "some_library"
print(lib1) --> table: 0xAAAAAAAAAAAA
print(lib2) --> table: 0xAAAAAAAAAAAA
  • You could use dofile which doesn't cache results. Unfortunately it doesn't support package.path or package.cpath, but you could technically parse those and find the includable file:

    local lib1 = dofile "/home/name/projects/lua/test/some_library.lua" local lib2 = dofile "/home/name/projects/lua/test/some_library.lua" print(lib1) --> table: 0xAAAAAAAAAAAA print(lib2) --> table: 0xBBBBBBBBBBBB

  • Another option is to delete the cached data, but I've never experimented with that, so I'm not sure if there are any unforeseen drawbacks:

    local lib1 = require "some_library" package.loaded.some_library = nil -- Remove cache added by the first require local lib2 = require "some_library" print(lib1) --> table: 0xAAAAAAAAAAAA print(lib2) --> table: 0xBBBBBBBBBBBB

/r/lua Thread