lua-users home
lua-l archive

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


> Now the question is, how do I access the value attribute without mentioning it.

Short answer: You don't.

Lua allows some ability to provide equality functions for your types through the __eq, __lt, __le metamethods, but they can only be used to compare objects sharing the same metamethod. So comparing your DeviceTag type to a string won't work. This is a little less flexible than the operator overloading C++ provides, but I've found overloading a la C++ leads to more time wasted debugging than time in keystrokes saved.

While DeviceTag.value is a little more verbose, it's intent is clear. Alternatively you might create an explicit comparison method.


-Michael


On Nov 10, 2010, at 11:13 PM, Gopalakrishnan Subramani wrote:

> Hello All,
> 
> We have Lua table definition like this.
> 
> DeviceTag = VARIABLE
> {
>     name = "DeviceTag",
>     data_type = Float,
>     address = 100
> }
> 
> We have written data access layer which shall be used like this.
> 
> DeviceDataSource.Read(DeviceTag);
> 
> the function Read is written in C++(COM)/C#. It reads the value from device and set the value to DeviceTag
> 
> DeviceTag.value = <<Data read from device>>
> 
> We will be using the DeviceTag.value for arithmetic operations, comparisons.
> 
> Now the question is, how do I access the value attribute without mentioning it.
> 
> like this 
> 
> if (DeviceTag == "Krish") then 
> 
>   do something here
> 
> end
> 
> Whereas when I say DeviceTag during comparison, it should provide me the DeviceTag.value by default.
> 
> Something like operator overload is possible to get this? Our attempt is, there a Device Description Language available and we want to generate our Lua code from existing Device Description Language. 
> I know Lua is general purpose and DSL defined must confirm Lua syntax. I am curious to learn Lua and creating DSL for our company.
> 
> Regards,
> 
> Gopalakrishnan
> 
> 
> 
> 
>