teal-language / tl

The compiler for Teal, a typed dialect of Lua
MIT License
2.02k stars 101 forks source link

weird error with variadic map argument #715

Closed fperrad closed 8 months ago

fperrad commented 8 months ago

first, with the type {string:number}, all is fine

local function foo(...: {string:number})  -- number values in the hash part of the table
   local t = {...}
   local sum = 0.0
   for i = 1, #t do
      for _, v in pairs(t[i]) do
         sum = sum + v
      end
   end
   print(sum)
end

foo({ a = 1 })                  --> 1.0
foo({ a = 1, b = 2 })           --> 3.0
foo({ a = 1 }, { a = 2 })       --> 3.0
foo({ a = 1 }, { b = 2 })       --> 3.0

but with the type {integer|string:number}, errors come when more than one table

local function foo(...: {integer|string:number})  -- number values in the array part and the hash part of the table
   local t = {...}
   local sum = 0.0
   for i = 1, #t do
      for _, v in pairs(t[i]) do
         sum = sum + v
      end
   end
   print(sum)
end

foo({ 42.0, a = 1 })                    --> 43.0
foo({ 42.0, a = 1, b = 2 })             --> 45.0
foo({ 42.0, a = 1 }, { 42.0, a = 2 })   -- ERROR: argument 2: can't match a record to a map with non-string keys
foo({ 42.0, a = 1 }, { 42.0, b = 2 })   -- ERROR: argument 2: can't match a record to a map with non-string keys
foo({ a = 1 }, { a = 2 })               -- ERROR: argument 2: can't match a record to a map with non-string keys
foo({ a = 1 }, { b = 2 })               -- ERROR: argument 2: can't match a record to a map with non-string keys
hishamhm commented 8 months ago

@fperrad Thank you for the report! It's now fixed.