lua-users home
lua-l archive

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


@Jonathan,

I am trying to index the output of the function. I define parameters with unit with 'new' subfunction such as:
pv=require('physvalue')
myElec = pv:new('',450, 'W')

With this myElec is a table with 'value' and 'unit' indices. However if I try to index it such as 
myElec.unit

I get nil although in the 'unit' index of the table output of the function it is 'W'


2016-10-17 10:29 GMT+02:00 Jonathan Goble <jcgoble3@gmail.com>:
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 ?