lua-users home
lua-l archive

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


That's how they deliberately designed it:

http://www.lua.org/manual/5.1/manual.html#2.2.1

String and number conversion is as far as they were comfortable doing
automatically. "true" and "false" as strings don't really mean as much
as numbers in strings do. And would you convert nil automatically to
"nil" or to ""?

Unless performance is absolutely critical (in which case you'll know
what are strings and what aren't), tostring() does what you want, and
doesn't mind strings themselves as arguments.


On 28/03/2008, Eugen-Andrei Gavriloaie <shiretu@gmail.com> wrote:
> Hi,
>
>  I think i found a problem regarding automatic conversion of data types
>  to string. Here is an example of a Lua script:
>
>  -- cut here --
>  numericValue=123.456
>  print("Type: ",type(numericValue))
>  print("Value: ",numericValue)
>  print("Some string "..numericValue)
>
>  boolValue=true
>  print("Type: ",type(boolValue))
>  print("Value: ",boolValue)
>  print("Some string "..boolValue)
>  -- end cut --
>
>  The output is:
>
>  -- cut here --
>  Type:   number
>  Value:  123.456
>  Some string 123.456
>  Type:   boolean
>  Value:  true
>  lua: aa:9: attempt to concatenate global 'boolValue' (a boolean value)
>  stack traceback:
>         aa:9: in main chunk
>         [C]: ?
>  -- end cut --
>
>  I see that for number type there is an automatic conversion to string.
>  Why boolean makes an exception?
>
>  Thank you
>