我正在寻找一个在lua中允许你拥有自定义变量类型的库/函数(甚至可以使用“type”方法检测为您的自定义类型)。我正在尝试制作一个具有自定义类型“json”的json编码器/解码器。我希望能够在lua中单独实现这个解决方案。
您无法创建新的Lua类型,但是您可以使用元表和表格来模拟它们的创建,以很大程度上达到相同的效果。例如:
local frobnicator_metatable = {}
frobnicator_metatable.__index = frobnicator_metatable
function frobnicator_metatable.ToString( self )
return "Frobnicator object\n"
.. " field1 = " .. tostring( self.field1 ) .. "\n"
.. " field2 = " .. tostring( self.field2 )
end
local function NewFrobnicator( arg1, arg2 )
local obj = { field1 = arg1, field2 = arg2 }
return setmetatable( obj, frobnicator_metatable )
end
local original_type = type -- saves `type` function
-- monkey patch type function
type = function( obj )
local otype = original_type( obj )
if otype == "table" and getmetatable( obj ) == frobnicator_metatable then
return "frobnicator"
end
return otype
end
local x = NewFrobnicator()
local y = NewFrobnicator( 1, "hello" )
print( x )
print( y )
print( "----" )
print( "The type of x is: " .. type(x) )
print( "The type of y is: " .. type(y) )
print( "----" )
print( x:ToString() )
print( y:ToString() )
print( "----" )
print( type( "hello!" ) ) -- just to see it works as usual
print( type( {} ) ) -- just to see it works as usual
输出:
table: 004649D0 table: 004649F8 ---- x的类型是:frobnicator y的类型是:frobnicator ---- frobnicator对象 field1 = nil field2 = nil frobnicator对象 field1 = 1 field2 = hello ---- string类型 table类型
当然,这只是一个简单的示例,关于Lua面向对象编程还有更多需要了解的内容。您可以参考以下参考资料:
jsonCode = {"json text here"} varType(jsonCode, "json")
将返回带有自定义类型"json"
的jsonCode
。tostring(jsonCode)
只会返回jsonCode[1]
(在这种情况下,它将等于"json text here"
)。不必实现此处的 2 值系统,我只会将其用于具有 1 个值的表格。 - Potassium Ion