[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: References (as in Perl)
- From: Rici Lake <lua@...>
- Date: Mon, 18 Dec 2006 13:06:34 -0500
On 18-Dec-06, at 12:20 PM, Gavin Kistner wrote:
"There are eight basic types in Lua: nil, boolean, number, string,
function, userdata, thread, and table.
Tables, functions, threads, and (full) userdata values are objects:
variables do not actually contain these values, only references to
I think that quote from the manual is a bit misleading, personally; but
I suppose it depends on how you interpret the word "object" (or, for
that matter, "variable").
My layman interpretation of the second sentence above was that
thus *do* 'contain' values that are not object types. (I realize that
this is technically a logical fallacy, but a reasonable interpretation
of the intent.) Thus, I had thought that the code...
local a = "foo"
local b = "foo"
local c = b
...meant that 3 strings were allocated in memory internally, one to be
Are 3 strings allocated and maintained internally? (Each assignment of
non-object value copies it 'into' the variable.)
Or is it 2? (One for each string literal, with the latter shared by 'b'
Or is it 1? (Are Lua's internals tricky enough that they 'hash' a
and only allocate memory for each unique sequence?)
Only one. Strings are indeed hashed and only unique sequences are
stored in memory.
As you say, it's an implementation detail, but one of the consequences
is that string equality comparisons are simply a pointer comparison,
and hence as fast as any other primitive equality comparison.