lua-users home
lua-l archive

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


        -- I want to write code like this
        v = new( "Vector" );
        v:SetNull();
        v.x = 0.2;      -- for now, i must write:       v.x:Parse( CreateString( "0.2" ) );
        v.y = 1.5;      -- for now, i must write:       v.y:Parse( CreateString( "1.5" ) );

        -- i need to hook to the assignment operation in order to correctly set
v.x.

Just use the __newindex metamethod. It could look like this:

function __newindex(u, k, v)
 local class = getmetatable(u)
 local mutator = class.mutators[k]
 if mutator then
   mutator(u, v)
 else
   error(string.format("missing mutator %s in class %s", tostring(k),
tostring(class)), 2)
 end
end