lua-users home
lua-l archive

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


On Fri, Oct 14, 2016 at 8:35 AM, Hur Butun <hurbutun@gmail.com> wrote:
> I needed to make some modifications to output a table instead of string. Now
> after my minor modifications the function outputs a table with 'value' and
> 'unit' indices (see the modified function in the attachment). However I
> cannot index the output table with 'unit' afterwards, it always returns to
> nil.

This is what I get:

>>> p = require 'physvalue'
>>> p.u.m:format().unit
m
>>> p.u.m:format().value
1.0

Exactly what are you trying to index? In this example, if you are
trying to do `p.u.m.unit', that doesn't work because the table you
want is only produced on demand by calling the `format()` method. The
`unit` and `value` fields are not present in the object itself.

Also, note that the `__tostring()` metamethod currently delegates to
`format()`, so the result of (in this example) `print(p.u.m)` is an
error:

>>> print(p.u.m)
stdin:1: 'tostring' must return a string to 'print'
stack traceback:
        [C]: in function 'print'
        stdin:1: in main chunk
        [C]: in ?