lua-users home
lua-l archive

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

Sincere thanks.
>>I would dearly love a slow, painstakingly detailed explanation of
>>the metable implementation.
>File handles are represented as userdata. To be able to use the syntax
>f:read() on for file handles, we must set an __index metamethod for file
>handles. We do this by adding an "__index" field to the metatable of file
>handles. The value of this field is the table itself because that's where we
>store the read functions and its friends. (Note that he value of the "__index"
>field can be a function or a table; if it is a table the fields are resolved
>in the given table.) We might have stored read and friends in another table,
>but metatables are ordinary tables so we use it.
>The fields "_input" and "_output" are used to store the current values of
>file handles returned by io.input() and io.output().
>A similar, but simpler, technique is used in my bindings for C libraries that
>receive handles as their first argument. For instance, you can write both
>md5.digest(d) or d:digest(), where d is an md5 context, because the metatable
>for md5 contexts has an "__index" field that points to the md5 table. So,
>d:digest(), which is sugar for d.digest(d), gets translated to md5.digest(d).