lua-users home
lua-l archive

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


On 10 Nov 2012, at 08:08, Dirk Laurie wrote:

> What other ways are there to do the test?

I don't quite follow the situation you describe, but I think you're saying that each object created by the library has a different metatable, even though it is the same type?

Is it possible to add a wrapper around the library that basically says:

--Library
local function library_f()
	--Returns objects of type "t1" but each with a different metatable
end
local function library_g()
	--Returns objects of type "t2" but each with a different metatable
end

--Wrapper
local wrapper_getType = nil
local wrapper_f = nil
local wrapper_g = nil
do
	local typeLookup = {}

	wrapper_f = function()
		local rv = library_f()
		typeLookup[getmetatable(rv)] = "t1"
		return rv
	end

	wrapper_g = function()
		local rv = library_g()
		typeLookup[getmetatable(rv)] = "t2"
		return rv
	end

	wrapper_getType = function(t)
		return typeLookup[getmetatable(t)]
	end
end

Thanks,
Kev