lua-users home
lua-l archive

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


I am sorry, indeed I should have given an example. 

With 'new' subfunction I can define a value with physical unit and the convert it to another unit. 

So if I define an example like this:
pv=require('physvalue')
local example=pv:new('',450, 'W')
print(example.unit)

'example' is a table with 'unit' and 'value' indices if I display it using a debugger. However when I try to index 'example' with 'unit' I get nil. 

I am attaching the example if you want to test it yourself. 




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 ?


Attachment: example.lua
Description: Binary data

Attachment: middleclass.lua
Description: Binary data

Attachment: MIT-LICENSE.lua
Description: Binary data

Attachment: physvalue.lua
Description: Binary data