[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: about tables, Table and metatables :o)
- From: Coda Highland <chighland@...>
- Date: Wed, 17 Jun 2015 20:46:52 -0700
On Wed, Jun 17, 2015 at 8:44 PM, Coda Highland <chighland@gmail.com> wrote:
> On Wed, Jun 17, 2015 at 7:56 PM, voidptr69@hotmail.com
> <voidptr69@hotmail.com> wrote:
>>
>> Another noob tought.
>>
>>
>> t = { "a", "b", "c" }
>> s = t:concat() -- notworking same has s= t.concat(t)
>> s = Table.concat(t)
>>
>> tables don't have metatable...
>> but why ? :o)
>>
>> My guess is 99% of tables in script are used like a default table and could
>> share Table by default..
>> And keep the short calling way valid is that case.
>>
>> When you need to use tables for others purpose;
>> class base or prototype base languages, mockup object, other data structures
>> etc etc ...
>> you will need to define a metatable and add the others functions you need (
>> constructor, copier, destructor ...)
>> so in those case you need to have an explicit constructor.
>>
>> so why not offer by default, like for String, a metatable for table ?
>>
>> voidptr :-)
>>
>
> If tables had metatables, then what data structure would you use to
> make objects with customized behavior?
>
> Tables are THE fundamental object type in Lua. Their entire purpose is
> to be simple and generic enough to be able to do anything you want
> with them.
>
> If you want t:concat() to work like table.concat(t), then
> setmetatable(t, table). You could even do something like this:
>
> function make_array(t)
> return setmetatable(t, table)
> end
>
> local t = make_array { "a", "b", "c" }
> print t:concat()
>
> /s/ Adam
Er. I screwed that up. It's setmetatable(t, { __index = table }).
/s/ Adam