__newindex setting table

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

__newindex setting table

Victor Bombi
Hi,

Where in the docs can be infered that

local aa = metatable({},{__newindex = function(t,i,v) t=v end})

aa[0] = 24

is not performing aa = 24

Best
victor bobi

Reply | Threaded
Open this post in threaded view
|

Re: __newindex setting table

Duncan Cross
On Fri, Jun 30, 2017 at 9:50 AM, Victor Bombi <[hidden email]> wrote:

> Hi,
>
> Where in the docs can be infered that
>
> local aa = metatable({},{__newindex = function(t,i,v) t=v end})
>
> aa[0] = 24
>
> is not performing aa = 24
>
> Best
> victor bobi
>

2.1 - Values and Types

"Tables, functions, threads, and (full) userdata values 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."


So, in your case, the parameter "t" and the variable "aa" are seperate
references, -- reassigning one reference does not affect the other.


-Duncan

Reply | Threaded
Open this post in threaded view
|

Re: __newindex setting table

Victor Bombi
Thanks and sorry for this silly question,.
__newindex function behaves the same as any function, of course.

----- Original Message -----
From: "Duncan Cross" <[hidden email]>
To: "Lua mailing list" <[hidden email]>
Sent: Friday, June 30, 2017 11:11 AM
Subject: Re: __newindex setting table


> On Fri, Jun 30, 2017 at 9:50 AM, Victor Bombi <[hidden email]>
> wrote:
>> Hi,
>>
>> Where in the docs can be infered that
>>
>> local aa = metatable({},{__newindex = function(t,i,v) t=v end})
>>
>> aa[0] = 24
>>
>> is not performing aa = 24
>>
>> Best
>> victor bobi
>>
>
> 2.1 - Values and Types
>
> "Tables, functions, threads, and (full) userdata values 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."
>
>
> So, in your case, the parameter "t" and the variable "aa" are seperate
> references, -- reassigning one reference does not affect the other.
>
>
> -Duncan
>