|
That's working as documented. From section 3.4 of the Lua
Reference
Both function calls and vararg expressions can result in multiple values. If a function call is used as a statement (see §3.3.6), then its return list is adjusted to zero elements, thus discarding all returned values. If an _expression_ is used as the last (or the only) element of a list of expressions, then no adjustment is made (unless the _expression_ is enclosed in parentheses). In all other contexts, Lua adjusts the result list to one element, either discarding all values except the first one or adding a single nil if there are no values.
Just below that in the list of examples:
a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil)
So in your string.format code, the
last %d really wants a number, but it got a nil so it threw an
error.
PS: The following also might be interesting:dofunction Test() return 3, 5 end s= string.format( "%d %d", Test(), 4) ends3 4 ... so it looks like the '...' of string.format somehow keeps only the first return value of Test, except if Test function is the last parameter... . Is this normal? (I thought, that for such "further parameter stripping" it would be required to specify this explicitely by brackets like in s= string.format( "%d %d", (Test()), 4)? (this gives the same as above, in this case it is clear to me...). Am Fr., 25. Nov. 2022 um 23:46 Uhr schrieb bil til <biltil52@gmail.com>:If I do the following in Lua 5.4:dofunction Test() return 3, 5 end s= string.format( "%d %d %d", 4, Test()) ends4 3 5 ... this is as expected...dofunction Test() return 3, 5 end s= string.format( "%d %d %d", Test(), 4) endstdin:3: bad argument #4 to 'format' (no value) stack traceback: [C]: in function 'string.format' stdin:3: in main chunk [C]: in ? ... this somehow does NOT work as I would expect... (I would expect that s now is '3 5 4') (if I debug with breakpoint in str_format, then the top of stack ( int top = lua_gettop(L);) is only 3 in this last case... in first case it is 4 as expected.... . Is this some error of Lua, or do I miss here something?