为Lua中的表键分配多个返回值的更好方法

时间:2017-01-01 18:36:42

标签: lua lua-table

假设我有一个返回多个值的函数。我碰巧与LÖVE的(Image):getDimensions合作。这将返回两个值,我知道这些值为width,height。我想将它们作为数组分配给新表。我想命名(字符串)键。因此,例如,我想将getDimensions()函数的返回值分别分配给具有键widthheight的新表。

我知道以下作品......

image = {}
image.data = love.graphics.newImage('myimage.png')
image.size = {}
image.size.width, image.size.height = image.data:getDimensions()

我想知道我是否可以使用任何类型的语法糖,或者使用标准库函数来使语法更符合......

image.size = { width, height = image.data:getDimensions() }

我知道上面这行不起作用,还有我尝试过的许多变化,包括使用unpack()的各种尝试。我是Lua的新手(现在约2天),所以也许还有另一个我不知道的标准函数或最佳实践,它会将一个键表与一个类似数组的表相关联。谢谢!

2 个答案:

答案 0 :(得分:3)

您可以编写自己的函数:

local function set_fields(tab, fields, ...)
   -- fields is an array of field names
   -- (use empty string to skip value at corresponging position)
   local values = {...}
   for k, field in ipairs(fields) do
      if field ~= "" then 
         tab[field] = values[k]
      end
   end
   return tab
end

local function get_fields(tab, fields)
   local values = {}
   for k, field in ipairs(fields) do
      values[k] = tab[field]
   end
   return (table.unpack or unpack)(values, 1, #fields)
end

用法示例#1:

image.size = set_fields({}, {"width", "height"}, image.data:getDimensions())

用法示例#2:
即时交换值!

local function get_weekdays()
   return "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
end
-- we want to save returned values in different order
local weekdays = set_fields({}, {7,1,2,3,4,5,6}, get_weekdays())
-- now weekdays contains {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}

用法示例#3:

local function get_coords_x_y_z()
   return 111, 222, 333  -- x, y, z of the point
end
-- we want to get the projection of the point on the ground plane
local projection = {y = 0}
-- projection.y will be preserved, projection.x and projection.z will be modified
set_fields(projection, {"x", "", "z"}, get_coords_x_y_z())
-- now projection contains {x = 111, y = 0, z = 333}

用法示例#4:
如果require("some_module")返回一个内部有大量功能的模块,但你只需要其中一些:

local bnot, band, bor = get_fields(require("bit"), {"bnot", "band", "bor"})

答案 1 :(得分:0)

使用类构造,我提出了以下内容......

Size = {}    
Size.mt = {}
Size.prototype = { width = 0, height = 0 }

function Size.new (dimensions)
  local size = setmetatable({}, Size.mt)

  if dimensions ~= nil and type(dimensions) == 'table' then
    for k,v in pairs(dimensions) do    
      size[k] = v
    end
  end

  return size
end

Size.mt.__index = function (t, k)
  if k == 'width' or k == 'height' then
    rawset(t, k, Size.prototype[k])

    return t[k]
  else
    return nil
  end
end

Size.mt.__newindex = function (t, k, v)
  if k == 1 or k == 'width' then
    rawset(t, 'width', v)
  elseif k == 2 or k == 'height' then
    rawset(t, 'height', v)
  end
end

然后我可以用多种方式初始化Size对象

  • 使用多个返回值:
    • image.size = Size.new{image.data:getDimensions()}
    • image.size = Size.new(table.pack(image.data:getDimensions())
  • 使用默认值:
    • image.size = Size.new()
    • image.size = Size.new{}
    • image.size = Size.new({})
  • 使用混合数组和哈希表:
    • image.size = Size.new({height=20, width=30})
    • image.size = Size.new({height=20, 30})

这种方法与Egor(效用函数)有利有弊,如果没有简单的语法技巧或我不知道的现有函数,这就是我正在考虑的事情。

优点:

  • (个人)在Lua中使用OO结构的学习经历
  • 我可以限制表格中实际密钥的数量,同时通过扩展if/else逻辑__index__newindex中的接受值来添加这些密钥的“同义词” }
  • 表中字段的显式定义,无需担心将键表与值表同步(与通用实用程序函数一样)

缺点

  • 需要为我想要这种行为的每个数据结构重复此模式
  • 成本高昂,对消费者来说只是一个非常小的差异的很多开销

我确信我可以及时使这种方法更加健壮,但我会很感激任何反馈。