Lua Notes

scanRecord example
Login

populate a record by key order

local runs = 0
local function getRun()
  runs = runs + 1
  return runs
end

local rec = @record{
  a: integer,
  b: integer,
  c: string,
}

local function scanRecord(rectype: type <comptime>)
  local r: rectype
  ## for i=1, #rectype.value.fields do
    ## if rectype.value.fields[i].type.is_stringy then
      r.#|rectype.value.fields[i].name|# = string.format('%d', getRun())
    ## else
      r.#|rectype.value.fields[i].name|# = getRun()
    ## end
  ## end
  return r
end

require 'string'
local r = scanRecord(rec)
print(string.format('{a=%d, b=%d, c=%s}', r.a, r.b, r.c))