[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Functions Without Returns
- From: "Kristofer Karlsson" <kristofer.karlsson@...>
- Date: Fri, 25 Jan 2008 19:25:43 +0100
In the first case, you try to store zero values "nothing()" into one
variable "local result".
The lua behaviour is then to expand the return value list with enough
nils to fill up all the variables.
This is the same case as with:
function foo()
return 1
end
local x, y = foo()
-- here x is 1 and y is nil
On Jan 25, 2008 7:16 PM, Ken Smith <kgsmith@gmail.com> wrote:
> I ran into a bit of Lua behavior I'd like to discuss in the forum.
> Consider this example. (Tested under Lua 5.1.3 on Mac OS X.)
>
> cat<<EOF>test.lua
> function nothing()
> end
>
> local result = nothing()
>
> print(tostring(result))
>
> print(tostring(nothing()))
> EOF
>
> The first print yields, "nil", whereas the second one causes the
> following error.
>
> lua: test.lua:8: bad argument #1 to 'tostring' (value expected)
> stack traceback:
> [C]: in function 'tostring'
> test.lua:8: in main chunk
> [C]: ?
>
> The manual is clear that a function "without...a return
> statement...returns with no results" (2.5.9). But then the discussion
> of nil mentions that it "usually represents the absence of a useful
> value" (2.2).
>
> My expectation was that a function with no return would return no
> results in the form of a nil. Granted there is a conundrum, if a
> function returns nil, then it returns a result.
>
> The way I see it, a function without a return returns no values which
> introduces another, distinct nil concept into the Lua runtime. There
> is nil, which is a placeholder value meaning, "different from all
> other values", then there is true nil as is generated by a function
> with no return which is the true absence of any value.
>
> Clearly, the workarounds are unobtrusive and numerous. However, I
> find this curious, so I'd like to hear a few more opinions on this
> issue. Should a function with no return statement return 'nil', or
> nothing? Are they different semantically?
>
> Ken Smith
>