我有一个帮助函数,它返回一个像这样的表:
function of_type(expected_type)
return {
expected = expected_type,
matches = function(value) return type(value) == expected_type end,
describe = "type " .. expected_type
}
end
现在这对其他匹配器来说很好,但是在这里我想在调用函数type(value)
时将其存储到同一个表中的一个字段。matches
像这样的东西:
function of_type(expected_type)
return {
expected = expected_type,
mismatch = nil, -- set it to nil on initialization
matches = function(value)
mismatch = type(value) -- how to do this?
return type(value) == expected_type
end,
describe = "type " .. expected_type
}
end
这可能吗?