lua-users home
lua-l archive

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


 I think this will suffice for my needs.
function tostringx(...) return ... ~= nil and tostring(...) or select('#', ...) == 0 and "none" or "nil" end
And yes the seldom mentioned (expr) adjustment is also a solution (I find myself most often using this with gsub to drop that second return).
I guess my issue is really about preference, should a function that converts lua values to a readable form be to render the nothing value. (or maybe cynically it's fine to not give vararg related things enough love ;) ).

On Tue, Mar 5, 2019 at 5:34 PM Dirk Laurie <dirk.laurie@gmail.com> wrote:
Op Di. 5 Mrt. 2019 om 19:18 het Gé Weijers <ge@weijers.org> geskryf:
>
> On Mon, Mar 4, 2019 at 9:11 PM Magicks M <m4gicks@gmail.com> wrote:
>>
>> function f() end
>> tostring(f()) --what happens here?
>> This code causes an error, which I think is not obvious, with the message "bad argument #1 to 'tostring' (value expected)" I believe this is because the number of values returned by f is 0 which means the vararg passed into tostring has length 0.
>>
>
> Even stranger: this works:
>
> tostring( ( f() ) )
>
> The extra parentheses adjust the number of return values of 'f' to be 1, introducing a nil value.

Not stranger! "Even better illustrating the inner logic of Lua, ..."
is more like it.

We have seen many posts on this list, including some by myself :-(
with the following scenario:

1. Think that you understand Lua.
2. Encounter something that does not fit in with the way you understand it.
3. Report "bug" or "error" on the list.

The solution to all of these is:

1. Read the manual.
2. Take into account what is elsewhere in the manual, not just the
place where you looked first.
3. Argue like a rabbi or a Jesuit to extract the finest possible nuamces.

And voilà! Everything is crystal clear, refined but malicious.

So I really like your example. It illustrates a fine point clearly.