lua-users home
lua-l archive

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


The pressing question for most of us is productivity rather than
processing time. Consider the following situation: somewhere
in my program, I have the statement

   full_name = first_name .. " " .. surname

Nobody reading the program is going to think there is
a mistake.

Unfortunately, some distance earlier, I gave a value to "firstname",
not "first_name". At present, at this very statement, Lua
throws an error because first_name is nil, I find the mistake
immediately and correct it.

How would it be if the first time I notice this mistake is when
annoyed clients ring up one after the other to tell me their names
are not "nil Zoller" etc?



2014-08-25 7:51 GMT+02:00 Dirk Zoller <duz@sol-3.de>:
> Hi All,
>
> Looking at Lua-Reference for 5.2 section 3.4.2.
> Coercion happens to numbers when used where a string is expected.
> It is done the same way as tostring() would.
>
> Why only to numbers and not to all other things, tostring() gives an answer
> for,
> like specifically "nil", and "true"/"false"?
>
> Timing it, I find that
>
>    "text " .. x
>
> with x a number is measurably faster than
>
>    "text " .. tostring(x)
>
> Having seen concatenation fail on nil or booleans, some people develop the
> habit to tostring() everything, including numbers.
>
> I think, the concatenation (and %s in format and ...)
> should uniformly tostring() everything that is possible.
>
> Thanks
> Dirk
>