tl icon indicating copy to clipboard operation
tl copied to clipboard

edge case of checking of the number of return value

Open fperrad opened this issue 1 year ago • 2 comments

local function bar()
end

local function foo()
   if true then
      return bar()  -- ERROR: in return value: excess return values, expected 0 (), got 1 (())
   else
      bar()
      return  -- OK but not idiomatic
   end
end

Note: with nil as explicit return, the checker is happy

local function bar(): nil
end

local function foo(): nil
   return bar()
end

fperrad avatar Mar 24 '24 07:03 fperrad

Thanks for the report! This is definitely a bug that needs fixing.

return bar()

Is the goal here to trigger a tail call? It surprised me a bit which of the two options you marked as idiomatic — I usually never return f() from 0-ary functions, to help the reader spot that nothing is being returned.

hishamhm avatar Mar 25 '24 14:03 hishamhm

I guess it varies but I tend to agree with @fperrad: I always return f() from 0-ary functions in Lua, in part because of the tail call.

catwell avatar Mar 25 '24 15:03 catwell