lua-users home
lua-l archive

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

On Mar 28, 2008, at 5:47 AM, Vaughan McAlley wrote:

That's how they deliberately designed it:

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.
IMHO anything can be converted to a string. Not vice-versa of course. I myself when writing a c++ class for example I always implement operator string() to
be able to rapidly dump the class in a human readable form for debug
purposes. In fact, is a reflex. Btw, I discovered this exception when dumping
some variables.

And would you convert nil automatically to
"nil" or to ""?
This is already happening.

> a=this_variable_does_not_exists
> print(a)

Moreover, this is extracted from the docs:

"Conversely, whenever a number is used where a string is expected, the number is converted to a string, in a reasonable format."

So, they adopted a "reasonable" format for converting numbers to string. I think it is "reasonable" enough to adopt a format for boolean values too. Even more complex data types like tables have a default format for converting to string. And IMHO is enough to inform
you that you have to deal with a table:

> a={}
> print(a)
table: 0x10b1b0

I think the question remains: Why boolean variables are so special? Am I missing something important?

Thank you

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 <> wrote:

I think i found a problem regarding automatic conversion of data types
to string. Here is an example of a Lua script:

-- cut here --
print("Type: ",type(numericValue))
print("Value: ",numericValue)
print("Some string "..numericValue)

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