lua-users home
lua-l archive

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


One case where I've actually encountered unavoidable issues with a Lua design due to type coercion was something like this, if I recall correctly:

num_table{[2]="This gets printed instead"}

index_searched_for="2"

print(num_table[index_searched_for] or "Even though this is what should be printed!")

If I've got that right and am not mis-remembering the problem, then Lua 5.2 should make the change that type coercion never comes into play when indexing tables.

On Thu, 15 Apr 2010 21:07:21 -0600, Henk Boom <henk@henk.ca> wrote:

I just fixed a subtle bug in my serialization/deserialization
functions for a data structure, where objects that were serialized
then deserialized looked identical but behaved slightly differently.
It was caused by something like this:

return 9 < 10
true
return "9" < "10"
false

Lesson learned: make sure to use tonumber() on numbers you extract
from strings...

It also boost the importance of frequent type checking in my mind, and
makes me hate automatic string->number coercion a little bit more :p

    henk