自定义变量类型Lua

8

我正在寻找一个在lua中允许你拥有自定义变量类型的库/函数(甚至可以使用“type”方法检测为您的自定义类型)。我正在尝试制作一个具有自定义类型“json”的json编码器/解码器。我希望能够在lua中单独实现这个解决方案。

2个回答

15

您无法创建新的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面向对象编程还有更多需要了解的内容。您可以参考以下参考资料:


针对您的type()函数,我的常规实现方式是在元表中实现一个__type字段来描述类名。这样,对于所有的类,type函数将始终是一个有用的值。 - Ian
@Zeksie 是的,这绝对是一个选项。正如我所说,关于Lua中的面向对象编程还有很多要说的。 - Lorenzo Donati support Ukraine
你能否编写一个函数来创建自定义变量类型?例如:jsonCode = {"json text here"} varType(jsonCode, "json") 将返回带有自定义类型 "json"jsonCodetostring(jsonCode) 只会返回 jsonCode[1](在这种情况下,它将等于 "json text here")。不必实现此处的 2 值系统,我只会将其用于具有 1 个值的表格。 - Potassium Ion

5
你所要求的是不可能的,即使使用C API也无法实现。Lua只有内置类型,不能添加更多类型。但是,使用元方法和表格,你可以创建函数来制作表格,这些表格非常接近其他语言中的自定义类型/类。例如,请参见《Lua 编程》- 面向对象编程(但请记住该书是针对Lua 5.0编写的,所以某些细节可能已经发生了变化)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接