lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


A comparison will always except one argument, so a function call going
straight into a comparison will adjust to one return value. Similarly,
wrapping an expression in a pair of brackets will adjust it to
argument. Your example in your opening post is one example where it is
not explicit - a function call as the last item in a paramater list /
argument list will not adjust the return values:

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> =type(string.byte("")) -- the result(s) of string.byte() are the last parameter(s) to type()
stdin:1: bad argument #1 to 'type' (value expected)
stack traceback:
        [C]: in function 'type'
        stdin:1: in main chunk
        [C]: ?
> =type((string.byte(""))) -- the extra () adjusts to 1 argument
nil
> =type(string.byte("") == 42) -- a comparison expects 1 argument
boolean
>

On Sun, Jan 25, 2009 at 12:56 AM, Vaughan McAlley <ockegheim@gmail.com> wrote:
> OK, so to fix this problem I need to tell it exactly how many results
> I want. My function:
>
> function f(s)
>    if string.byte(s) == 42 then
>        -- do stuff if and only if s starts with "*"
>    end
> end
>
> Should be instead:
>
> function f(s)
>    local b = string.byte(s) -- make the number of results expected explicit
>    if b == 42 then -- etc
>
> Thanks,
> Vaughan
>
> 2009/1/25 Alexander Gladysh <agladysh@gmail.com>:
>> On Sun, Jan 25, 2009 at 12:30 AM, Peter Cawley <lua@corsix.org> wrote:
>>> On Sat, Jan 24, 2009 at 9:08 PM, Alexander Gladysh <agladysh@gmail.com> wrote:
>>>> In Lua "to return zero values" is the same as to return nil.
>>> No, they are not the same. The number of return values from a function
>>> call is corrected to number of results which the caller is expecting
>>> either by appending nils, or truncating the last value(s). In many
>>> contexts, like assigning the results of a function to variables, or
>>> performing a table index on the result of a function, the number of
>>> results is set explicitly by the caller. In other contexts, like the
>>> end of an argument list for function calls and table constructions, no
>>> appending or truncating is done.
>>
>>> Hence the visible behaviour of returning zero values is the same
>>> visible behaviour as returning nil, in certain contexts. In all
>>> contexts, the non-visible behaviour is different, and in some
>>> contexts, the visible behaviour is different.
>>
>> Wow. You're right.
>>
>> $ lua
>> Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
>>> f = function() end
>>> print(type(f()))
>> stdin:1: bad argument #1 to 'type' (value expected)
>> stack traceback:
>>        [C]: in function 'type'
>>        stdin:1: in main chunk
>>        [C]: ?
>>> ff = function() return nil end
>>> print(type(ff()))
>> nil
>>
>> I didn't know this.
>>
>> Thank you,
>> Alexander.
>>
>