lua-users home
lua-l archive

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


On 24 April 2017 at 13:32, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
>> (Side note: string.byte is a bit funny in that it returns _no value_
>> in case of overflow (not nil)
>>
>> > string.byte("hello", 10)
>> > type(string.byte("hello", 10))
>> stdin:1: bad argument #1 to 'type' (value expected)
>> stack traceback:
>>     [C]: in function 'type'
>>     stdin:1: in main chunk
>>     [C]: in ?
>> > x = string.byte("hello", 10); print(x)
>> nil
>>
>> It's not clear to me what are the criteria for functions to return no
>> value versus nil -- as seen above, string.find returns an explicit
>> nil.)
>
> string.byte has a variable number of returns:
>
>> string.byte("hello", 3, 5)
>   --> 108       108     111
>> string.byte("hello", 5, 5)
>   --> 111
>> string.byte("hello", 6, 5)
>   --> (nothing)
>
> If there are no results to be returned, it returns nothing.

Got it. Following that logic, I was able to successfully conclude
table.unpack({}) would behave the same and indeed it does. :)

Also, I had never noticed before that parentheses coerce nothing to
nil. Learn something new every day, even in a small language like Lua!

> type((string.byte("hello", 10)))
nil

-- Hisham