在Lua中访问表键赋值右侧的值(字段)中的键

目标是将表的键与取决于该键的值匹配。

example = { ["dummy"] = this .. " example" }
print example.dummy -- print "dummy example"

其中 this 是关键字,用于引用键。 在Lua中是否有任何方法可以执行该操作?

点赞
用户107090
用户107090

这个问题无法直接解决。

你可以进行一些预处理:

example = { ["dummy"] = "{THIS} example" }
for k,v in pairs(example) do
    example[k]=v:gsub("{THIS}",k)
end
print(example.dummy)
2016-11-17 11:22:53
用户2505965
用户2505965

由于 this 总是表达为与键或表无关的关系,因此不可能像下面那样使用干净的表达式:

t = { foo = this .. ' bar' }

也就是说,您不能将表条目的值捕获为表达式。

但是可以通过 元表 和函数实现某种级别的间接引用,但这不是很美观。这里我们进行获取时的计算。您也可以重新计算结果。

local function indirect_table ()
    local uptable = {}

    return setmetatable({}, {
        __index = function (self, key)
            local value = uptable[key]

            return type(value) == 'function' and uptable[key](key) or value
        end,
        __newindex = function (self, key, value)
            uptable[key] = value
            --[[预先计算,无需 uptable 或 __index:
                `rawset(self, key, value(key)`]]
        end
    })
end

local tab = indirect_table()

tab.foo = function (key) return key .. 'bar' end

print(tab.foo) --> 'foobar'

注:此示例使用闭包,但您也可以使用 getmetatable 实现此类模式。


个人而言,我将其抽象为间接引用模式,允许指定任意键和值以及它们的 _操作_。我认为这种模式主要会在程序中使用,而不是手动使用,其中键值的结果取决于接收到的输入。再次说明,这不太美观,但更加稳健(可选的操作)。

local function I (_, value) return value end

local K = setmetatable({
    __call = function (actor, key)
        return actor.action(key, actor.value)
    end
}, {
    __call = function (K, value, action)
        return setmetatable({ value = value, action = action or  I }, K)
    end
})

local T = setmetatable({
    __newindex = function (self, key, value)
        if getmetatable(value) == K then
            value = value(key)
        end

        rawset(self, key, value)
    end
}, {
    __call = function (T, o)
        return setmetatable(o or {}, T)
    end
})

简单用法:

local function concat (left, right) return left .. right end

local t = T {}

t.foo = K('bar', concat) -- 具有共同操作
t.zar = K({}, unknown_action) -- 没有操作( `nil`)
t.qux = 'qaz' -- 标准

print(t.foo, t.zar, t.qux)

这是奇怪的元编程。我会再次检查需要这种方法的原因。也许您陷入了 XY 问题 陷阱?确实感觉像是解决问题的解决方案,其本身没有必要存在。

2016-11-17 16:36:30