documentation: metatable is a reference (refman is unclear)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

documentation: metatable is a reference (refman is unclear)

nobody
Hey *,

(unless I fail at searching), the reference manual does not explicitly
state that metatables are "stored" by reference.  (I.e. `setmetatable`
doesn't copy the thing passed as metatable, but puts a reference to it
in a suitable location.)

The closest thing to information on this is the next-to-last paragraph
of §2.1:

> "Tables, [...] are objects: variables do not actually contain these
> values, only references to them. Assignment, parameter passing, and
> function returns always manipulate references to such values; these
> operations do not imply any kind of copy."

While it's clear from this that `setmetatable` receives a reference to
the (soon-to-be) metatable, `setmetatable` could do anything with that
(keep the reference, create a copy, …)  To someone who knows Lua, it's
clear that it just stores the reference (simplicity and all that), but
§2.4 (Metatables and Metamethods) completely avoids the topic, and the
description for `setmetatable` and `lua_setmetatable` just states that
they "set" the metatable.

Should this be clarified?  (Judging from a recent question on SO, this
has the potential to confuse some people coming from other languages.)

-- nobody

Reply | Threaded
Open this post in threaded view
|

Re: documentation: metatable is a reference (refman is unclear)

Egor Skriptunoff-2
On Sun, May 7, 2017 at 1:02 PM, nobody wrote:

the reference manual does not explicitly state that metatables are "stored" by reference


Manual, section 2.4, second sentence: "metatable is an ordinary Lua table".
Is this not enough to clarify all about how metatables are being stored/set/unset?
Reply | Threaded
Open this post in threaded view
|

Re: documentation: metatable is a reference (refman is unclear)

Dirk Laurie-2
In reply to this post by nobody
2017-05-07 12:02 GMT+02:00 nobody <[hidden email]>:

> (unless I fail at searching), the reference manual does not explicitly
> state that metatables are "stored" by reference.  (I.e. `setmetatable`
> doesn't copy the thing passed as metatable, but puts a reference to it
> in a suitable location.)

There simply is nothng else in Lua except copy by reference.
Not even a table copy library function in a library.

> Should this be clarified?  (Judging from a recent question on SO, this
> has the potential to confuse some people coming from other languages.)

Please write a little document entitled "Lua for XXX programmers"
aimed at such people and post it on the list. i don't think the Lua team,
widely read though they are, can be expected to ancipate the sort of
remedial instruction that might be needed in various plausible cases,.

Reply | Threaded
Open this post in threaded view
|

Re: documentation: metatable is a reference (refman is unclear)

Ross Berteig
In reply to this post by nobody


On 5/7/2017 3:02 AM, nobody wrote:
> (unless I fail at searching), the reference manual does not explicitly
> state that metatables are "stored" by reference.  (I.e. `setmetatable`
> doesn't copy the thing passed as metatable, but puts a reference to it
> in a suitable location.)

The reference manual is very careful to be specific. If the table were
copied, it would say that because that is different from how tables are
used. It doesn't need to say that the table is not copied into internal
storage because that is the normal case in any other use of tables. (The
manual doesn't say that arithmetic never returns a string either. It
doesn't, but saying so would raise the question of when it might.)

One could argue that this habit does make the reference manual harder to
read for a language learner. But that is not the role of a reference
manual. See Programming in Lua for one example (and likely the best
example) of a language textbook that a learner should start with.

This lack of copying also follows from the way in which tables are
treated as values. Since

     a = {}
     b = a
     b.one = 1
     print(a.one)     -- prints "1"

is the way tables work in all other contexts, why would this be
particularly different:

     mt = {}
     c = setmetatable({}, mt)
     mt.one = 1
     print(getmetatable(c).one) -- prints "1"

The fact that tables are "stored by reference" is just an implementation
detail that makes the value semantics of tables practical to implement.
This is similar to strings being internalized, which is also just an
implementation detail that make their value semantics practical to
implement.

--
Ross Berteig                               [hidden email]
Cheshire Engineering Corp.           http://www.CheshireEng.com/
+1 626 303 1602


Reply | Threaded
Open this post in threaded view
|

Re: documentation: metatable is a reference (refman is unclear)

Gé Weijers


On Mon, May 8, 2017 at 3:02 PM, Ross Berteig <[hidden email]> wrote:

The fact that tables are "stored by reference" is just an implementation detail that makes the value semantics of tables practical to implement. This is similar to strings being internalized, which is also just an implementation detail that make their value semantics practical to implement.


Lua tables do not have value semantics, as you examples are clearly showing.

a={}
b=a
a.x = 1
b.x = 2
print(a.x) -- prints 2

This is reference semantics. You'd see '1' if Lua implemented value semantics.

String internalization is a different beast, because strings are immutable, so value semantics and reference semantics are indistinguishable if you ignore memory and complexity issues.


--
--

Reply | Threaded
Open this post in threaded view
|

Re: documentation: metatable is a reference (refman is unclear)

Coda Highland
On Mon, May 8, 2017 at 3:40 PM, Gé Weijers <[hidden email]> wrote:

>
>
> On Mon, May 8, 2017 at 3:02 PM, Ross Berteig <[hidden email]> wrote:
>>
>>
>> The fact that tables are "stored by reference" is just an implementation
>> detail that makes the value semantics of tables practical to implement. This
>> is similar to strings being internalized, which is also just an
>> implementation detail that make their value semantics practical to
>> implement.
>>
>
> Lua tables do not have value semantics, as you examples are clearly showing.
>
> a={}
> b=a
> a.x = 1
> b.x = 2
> print(a.x) -- prints 2
>
> This is reference semantics. You'd see '1' if Lua implemented value
> semantics.
>
> String internalization is a different beast, because strings are immutable,
> so value semantics and reference semantics are indistinguishable if you
> ignore memory and complexity issues.
>
>
> --
> --
> Gé

In the context, I understood that what he meant was "the semantics of
a table", not "tables have value semantics".

Yes, there is a semantic difference, and he was incorrect due to his
choice of words, but the actual reasoning is sound.

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: documentation: metatable is a reference (refman is unclear)

Dirk Laurie-2
In reply to this post by Ross Berteig
2017-05-09 0:02 GMT+02:00 Ross Berteig <[hidden email]>:

> The fact that tables are "stored by reference" is just an implementation
> detail that makes the value semantics of tables practical to implement.

I prefer to use the phrase "implementation detail"  for things that the
you don't need to know, and if you do, that you are not supposed to
rely on, like array part, hash part, the algorithms used to find table
boundaries and to sort, etc.

The fact that tables are "stored by reference" is a fundamantal part
of Lua and even the rankest newbie had better come to grips with
that very soon.