lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Javier,

very interesting... this is new (to me) a paradigm very different than
others I know (procedural, events and OO oriented)

today is my first day on the languagen, this is why I'm so-so
confused, but I'm liking this paradigm :) and all seems to be very
fast.

thank you for explain :)

PS: your name seems to be portuguese, this is why I asked :)

2008/7/14 Javier Guerra <javier@guerrag.com>:
> On Mon, Jul 14, 2008 at 3:31 PM, Marinho Brandao <marinho@gmail.com> wrote:
>> David,
>>
>> thank you for the quickly response :)
>>
>> well... I tried your dir function with a table and it printed the keys
>> and values for me (not the table's attributes and methods)
>>
>> and tried it with a userdata and an error has been raised:
>>
>> (read_query) cached_queries.lua:6: bad argument #1 to 'pairs' (table
>> expected, got userdata)
>>
>> Javier,
>>
>> thank you also... and maybe your message clarify the clouds are in my
>> mind :) well... if Lua hasn't objects, means this that most of data
>> types won't retrieve to me a map of their own attributes and methods?
>
> As David said, there aren't "attributes" and "methods", just members
> of a table. roughly equivalent to dict() objects in Python, but with
> nicer syntax.
>
> the syntax part is important: in Lua writing table.field is exactly
> the same as table["field"], and can be used interchangeably.  that
> lets you use tables as records with constant fields.  (in python the
> subscript syntax is somewhat cumbersome to use as records, and the
> 'attribute' syntax is nice, but has a different semantics)
>
> the other syntax sugar that you have to have in mind is the OO-like form:
>
> obj:methd(...)  === obj.methd(obj,....)
>
> that lets you use a table with functions as an 'object' with
> 'methods'. again, this is just "syntax sugar", and both forms compile
> to exactly the same bytecode.
>
> now the third surprise, that makes so easy to 'write your own' OO-like
> framework: if a Lua value (usually a table or userdata) can be
> extended by a "metatable", that is a table with several predefined
> keys that enhance the behaviour.  specifically, the "__index" field of
> a metatable lets you add 'extra' members to a table or userdata.  this
> is commonly used to store the 'methods' of a 'class', and then giving
> the same metatable to several values makes them 'objects' of the same
> 'class'
>
> you can get the metatable of an 'object' with getmetatable(obj), from
> there, you can check the __index field.  if it's a function, Lua calls
> it with the obj and index as parameters (a bit like __getitem__ in
> Python), if it's a table, Lua simply searches it after failing with
> the 'main' 'object'
>
> putting all together:
>
> let's say you have an object in variable 'obj', first check what type it is:
>
> print type(obj)
>
> if it's a table, you can enumerate it.  if it's a userdata, the
> 'contents' are hidden in C
> to enumerate a table:
>
> for k,v in pairs(obj) do print (k,v) end
>
> in most cases, you'll see the 'instance' data, not the 'class' data,
> so let's check the metatable:
>
> mt = getmetatable(obj)
> print (mt.__index)
>
> if mt.__index is a table, enumerate it.  you should find functions
> there.  these are the 'methods' that are called when you do
> obj:mth(...)
>
> maybe not all methods are there, in that case, check if there's
> another metatable:
>
> mt2 = getmetatable (mt.__index)
>
> or, maybe mt.__index isn't a table, but a function.... in that case,
> you can't enumerate.  go for the docs
>
>> I mean: in Python, I use dir() to list all that an object has and this
>> makes me knows that an object has the method that I need, and so on...
>> this makes me gain hours of research, just using the intuition.
>>
>> PS: if you are brazilian, let me know if I can send to you in PVT,
>> maybe using portuguese I can explain better my questions :)
>
> sorry, no portugese (but lots other do talk it).  in any case, your
> question is clear, just remember that Lua is not Python:
>
> "Python: batteries included" - Guido van Rossum
> "Lua gives you the power; you build the mechanisms" - Roberto Ierusalimschy
>
>
> --
> Javier
>



-- 
Marinho Brandão (José Mário)
http://marinho.webdoisonline.com/