Its okey or not?

49 views
Skip to first unread message

blogdron (BLOGDRON)

unread,
Feb 9, 2026, 11:21:45 AM (2 days ago) Feb 9
to lua-l
Test 1 OK
type(((''):byte())) 

Test 2 ERR bad argument #1 to 'type' (value expected)
type((''):byte()) 

Test 3 OK
string.byte = function (s) return nil  end
type((''):byte())

Test 4 ERR bad argument #1 to 'type' (value expected)
string.byte = function (s) return  end
type((''):byte())

"function x() return end" not equal "function x() return nil end"? 
This case is for all Lua versions.  

This behavior is probably well-known, but I've never encountered it before. I thought if a type received a nil, it would simply return a nil, but sometimes there's literally nothing there, not a nil. Now I'm paranoid about wrapping everything in parentheses, even if the function returns only one value :D  haha and/or if function return nothing im force wrote return nil

I thought I'd share. It seems I still don't know Lua ::)

Lars Müller

unread,
Feb 9, 2026, 11:29:22 AM (2 days ago) Feb 9
to lu...@googlegroups.com
What you're seeing is the difference between "nothing" (the empty vararg) and "nil".
In general, a vararg containing some nils is not the same as an empty vararg.
Thus varargs are not quite the same as tables, and you need to store their length when storing them in a table.

Have you ever wondered how "print()" prints just an empty line, whereas "print(nil)" prints "nil"? Well, now you know :)

I have a blog post that covers this: https://luatic.dev/posts/2025-04-12-lua-misconceptions/#varargs

- Lars
--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/ff009071-c489-4e98-9bc6-4046fcce63dbn%40googlegroups.com.

blogdron (BLOGDRON)

unread,
Feb 9, 2026, 11:33:09 AM (2 days ago) Feb 9
to lua-l
Yes, there's a first time for everything =) Thanks, I'll read your text. It's strange that I haven't noticed or encountered this before; I've been writing in Lua for several years now. LOL

понедельник, 9 февраля 2026 г. в 19:29:22 UTC+3, Lars Müller:

Javier Guerra Giraldez

unread,
Feb 9, 2026, 12:48:15 PM (2 days ago) Feb 9
to lu...@googlegroups.com
On Mon, 9 Feb 2026 at 11:21, blogdron (BLOGDRON) <blog...@gmail.com> wrote:
This behavior is probably well-known, but I've never encountered it before. I thought if a type received a nil, it would simply return a nil, but sometimes there's literally nothing there, not a nil. Now I'm paranoid about wrapping everything in parentheses, even if the function returns only one value :D  haha and/or if function return nothing im force wrote return nil

i think you diagnosed it right: it boils down to the difference between nil and "no arguments".

in this case it's a bit obscured because of the behaviour of string.byte() on empty strings.  of course, it's mathematically pleasing that the number or return values is the number of bytes in the input string.  and an empty string has 0 bytes.   this helps a lot on the case where you want to pass the results to a vararg function, or store in a table.  when passing to a function with a function with a required number of args... then yes, you get issues.

similarly, since a variable (or a function argument) can't hold "nothing", the difference between `type()` and `type(x)` is almost a syntactic one;  i guess that's the reason to raise an error instead of returning a `nil`,<errstring> pair.

so, is there any way to make it easier for a "casual" user that doesn't want to go into the language design of Lua?  i can't think of any right now.

--
Javier

Martin Eden

unread,
Feb 9, 2026, 1:06:14 PM (2 days ago) Feb 9
to lu...@googlegroups.com

On 2026-02-09 18:21, blogdron (BLOGDRON) wrote:
> "function x() return end" not equal "function x() return nil end"?
> This case is for all Lua versions.

Hello blogdron,

Lua functions are not crippled "functions" from C or FORTRAN.
They return sequence of values.

So "return nil, nil" is two values, "return nil" is one and "return"
is zero. You can even count this way. Somewhat similar to Church numerals.

Funny part is that parenthesis () around sequence always return
sequence with one element. So zero sequence becomes sequence with one
"nil":

  Lua 5.5.0  Copyright (C) 1994-2025 Lua.org, PUC-Rio
  > f = function() return end
  > print(f())

  > print((f()))
  nil

-- Martin

blogdron (BLOGDRON)

unread,
Feb 9, 2026, 1:58:47 PM (2 days ago) Feb 9
to lua-l
Thank you all ::)

понедельник, 9 февраля 2026 г. в 21:06:14 UTC+3, Martin Eden:
Reply all
Reply to author
Forward
0 new messages