[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Possible string conversion problem
- From: Eugen-Andrei Gavriloaie <shiretu@...>
- Date: Fri, 28 Mar 2008 07:23:55 +0200
On Mar 28, 2008, at 5:47 AM, Vaughan McAlley wrote:
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.
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)
nil
>
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 <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