andremm / typedlua

An Optional Type System for Lua
565 stars 53 forks source link

Prevent automatic variadic tail in strict mode for expression lists #82

Closed Veltas closed 8 years ago

Veltas commented 8 years ago

I tried compiling the factorial example from the paper (Typed Lua: An Optional Type System for Lua) shown below (changed for integers since I'm on 5.3):

local function factorial(n: integer): integer
  if n == 0 then
    return 1
  else
    return n * factorial(n - 1)
  end
end
local x = 5
print(factorial(x))

This program will not compile in strict mode, which is a bit counter-intuitive. It produces the following error:

typedtest.tl:1:25: type error, return type '(integer, nil*)' does not match '(integer)'

Which makes sense, as normally you'd want an expression list like the expressions in a return statement to have that type, right? Well I don't really know.

I made the change to not append the nil* on strict mode (and this allows the above code to compile). I don't know if this is an appropriate fix, if it breaks other things or not, but I made the PR for your consideration anyway.

EDIT: It's worth pointing out p.3 of the paper, which does make it seem like you should be able to work like this: "There is an optional stricter mode of operation where Typed Lua does not give variadic tails to the parts of function type unless the programmer explicitly declares it, and so will also check all function calls for arity mismatch." Without my change this is not possible for specifying the return part, without giving (... , nil*) manually (or automatically deducing).

Veltas commented 8 years ago

Hmmm... turns out this breaks basic assignment. I shall try "fixing" this again and make another PR.